Bug #5468
API JSON not correctly escaped?
0%
Description
The JSON which the API returns is as far as I can tell not correctly escaped concerning quotes in string fields and so not parseable.
http://server:9981/api/dvr/entry/grid_upcoming
[{"uuid":"4","disp_title":"Pink Floyd "P.U.L.S.E""}...]
Note the additional quotes in the title.
History
Updated by Jaroslav Kysela almost 6 years ago
It might be a client issue, the " character is escaped here: https://github.com/tvheadend/tvheadend/blob/master/src/htsbuf.c#L520
Updated by Thomas Ziegler almost 6 years ago
Sorry, a backslash got lost, the API returns sth like "Pink Floyd \"PULSE\"".
I try to do a JSON.parse() with the returned string and I get only exceptions (malformed JSON).
Online validators like https://www.w3schools.com/js/tryit.asp?filename=tryjson_parse_date where I enter something like
<!DOCTYPE html> <html> <body> <p id="demo1"></p> <p id="demo2"></p> <script> var text1 = '{"name":"\\\"John"}'; var text2 = '{"name":"\"John"}'; var obj1 = JSON.parse(text1); document.getElementById("demo1").innerText = "1:" + obj1.name; var obj2 = JSON.parse(text2); document.getElementById("demo2").innerText = "2:" + obj2.name; </script> </body> </html>
at least suggest to me, that the correct encoding would be an double backslash followed by an backslash quote. As text1 gets parsed, text2 not.
But I am unsure if this isn't a client issue representing json as a string or something like this, I found no conclusive answer to this on the net...
Updated by Jaroslav Kysela almost 6 years ago
For javascript text in your case, there's double escaping, so \\\" is converted to \" for the text variable (javascript parser) and then the text is parsed correctly with the json parser. Just put "alert(text1);" command right bellow the text1 variable initialiation and you'll see that you'll get just \" in the alert window. If you initialize javascript variable from the remote XML RPC request, you should have correct text for the json parser.
Updated by Thomas Ziegler almost 6 years ago
Jaroslav, you are right, an XMLHttpRequest works. What I try to achieve is connecting TVH to OpenHAB via the http1-Binding and JS-Transform, which does not work at the moment, but I keep investigating…
Issue can be CLOSED.