Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Illegal characters in JSON response

I have a Sencha Touch app. One of the stores I have uses an ajax proxy and a json reader. Some of the strings in the JSON returned from my sinatra app occasionally contain this character: http://www.fileformat.info/info/unicode/char/2028/index.htm

Although it's invisible, the character occurs twice in the second string here, between the period and the ending quote:

"description": "Each of the levels requires logic, skill, and brute force to crush the enemy.

"

Try copy and pasting "Each of the levels requires logic, skill, and brute force to crush the enemy.

" into your javascript console! It won't be parsed as a string, and fails with SyntaxError: Unexpected token ILLEGAL.

This causes the JSON response to fail. I've been stuck on this for a long time! Any suggestions?

like image 559
nnyby Avatar asked Apr 04 '12 22:04

nnyby


People also ask

What characters should be escaped in JSON?

In JSON the only characters you must escape are \, ", and control codes. Thus in order to escape your structure, you'll need a JSON specific function.

Is Unicode allowed in JSON?

JSON data always uses the Unicode character set.

Can JSON parse special characters?

parse needs to get a string which consists only of unicode characters (see Json parsing with unicode characters). For you the JSON. parse method fails, because your string contains non-unicode characters.


1 Answers

The only reliable way to fix this is server-side. Make sure your JSON generator emits those characters escaped, e.g. as \u2028.

In my experience, it's easiest to simply encode your JSON in plain ASCII which will always work. The downside is that it's less efficient as non-ASCII characters will take up more space, so depending on the frequency of those, you may not want that trade-off...

The documentation for Perl's JSON::XS has a good explanation of the problem and advice for how to fix it in Perl: http://search.cpan.org/perldoc?JSON::XS#JSON_and_ECMAscript

like image 195
Lasse Avatar answered Sep 21 '22 06:09

Lasse