5

I am using the YouTube API and I'm using Python urllib2.urlopen() to send a GET request. Then I pass the result to Javascript. (I'm using Django)

So, something like this:

result = urllib2.urlopen('https://gdata.youtube.com/feeds/api/videos?'+query+'&max-results=1&alt=json')

I'm using jQuery to parse the JSON formatted response, however some YouTube videos/descriptions have double quotes and this breaks the parseJSON() function.

Any help would be highly appreciated.

Mark Hildreth
  • 42,023
  • 11
  • 120
  • 109
volk
  • 1,196
  • 1
  • 12
  • 31
  • 2
    Example data? Tracebacks perhaps? – Martijn Pieters Aug 12 '12 at 23:12
  • for example... {"test": ""quoted material""} ...this wouldn't work, and that's the kind of result some of the Youtube videos return. There's quotes inside that aren't escaped, and this breaks the parser. – volk Aug 12 '12 at 23:14
  • does this mean i'm screwed and i need to hack away at some crazy code to try to catch those random quotes? – volk Aug 12 '12 at 23:16
  • 1
    Can you give an example query that returns such results? I have otherwise hard time believing that youtube would return invalid json.. – Esailija Aug 12 '12 at 23:21
  • 1
    @Esailija It's not that hard to imagine, unfortunately :( Historically, at least, Google [*gasp!*] and Facebook have returned invalid JSON from different services/queries. (But it is always nice to have *current live proof* of an invalid service.) –  Aug 12 '12 at 23:28
  • 1
    @Esailija http://stackoverflow.com/questions/11021041/detecting-and-parsing-escape-character-from-a-json-file (for instance) –  Aug 12 '12 at 23:31
  • 1
    Well yeah, but here's plenty of quotes and it's working fine http://jsfiddle.net/bZfqe/ I also verified that the json is valid in the unpadded version as well. – Esailija Aug 12 '12 at 23:33
  • @Esailija JSONLint also validates *that link* (I wish one could "share" a JSONLine paste). However, post says "some", so I wonder what such a "some" link is .. –  Aug 12 '12 at 23:35
  • @volk If an *exact* link to the JSON in question can be included .. with an *exact* paste of the result .. (Please *update the question*.) –  Aug 12 '12 at 23:37
  • okay guys, sorry for the waste of time... but the error was on my end (obviously) ..the error started with the fact that I didn't realize Django automatically escapes HTML characters due to security, and I hacked my own way of ignoring special HTML chars like """ , which ended up malforming the json. – volk Aug 12 '12 at 23:46
  • @pst haha, tbh that does not even try to be JSON. – Esailija Aug 12 '12 at 23:52

1 Answers1

2

the error was on my end (obviously) ..the error started with the fact that I didn't realize Django automatically escapes HTML characters due to security, and I hacked my own way of ignoring special HTML chars like & quot; which ended up malforming the json.

the easy fix (in case anybody uses django and ever runs into this problem) to escape special HTML chars is with {{ var|safe }} ..

volk
  • 1,196
  • 1
  • 12
  • 31