Project

General

Profile

Bug #5468

API JSON not correctly escaped?

Added by Thomas Ziegler almost 6 years ago. Updated almost 6 years ago.

Status:
Invalid
Priority:
Normal
Assignee:
-
Category:
API
Target version:
-
Start date:
2018-12-29
Due date:
% Done:

0%

Estimated time:
Found in version:
current git
Affected Versions:

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

#1

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

#2

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...

#3

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.

#4

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.

#5

Updated by Jaroslav Kysela almost 6 years ago

  • Status changed from New to Invalid

Also available in: Atom PDF