Illegal characters in JSON response

12,411

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

Share:
12,411
nnyby
Author by

nnyby

Updated on June 03, 2022

Comments

  • nnyby
    nnyby almost 2 years

    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?

  • Salman A
    Salman A about 12 years
    The code won't parse so you cannot fix it on the client side!
  • Salman A
    Salman A about 12 years
    I tried it in Chrome. Copied the portion of OP's code after : and pasted it in console, got syntax error.