0

Running Firefox 23.0.1, I have the following snippet:

var foo = '{  "success": false,  "errtype": "barf",  "message": "my message\n"}';

var what = JSON.parse(foo);
console.log(what);

Running this in the Firebug Javascript window, or watching the console log from my web page, I get SyntaxError: JSON.parse: bad control character in string literal.

But this seems like valid JSON to me, per http://www.json.org/ and it also passes the validator here: http://jsonlint.com/.

When I remove the "\n" embedded at the end of "my message", the problem in Firefox goes away. Haven't tried other browsers.

Is this a bug in Firefox?

Leonard
  • 13,269
  • 9
  • 45
  • 72
  • possible duplicate of [How do I handle newlines in json?](http://stackoverflow.com/questions/42068/how-do-i-handle-newlines-in-json) – zs2020 Aug 28 '13 at 05:19
  • 1
    That `\n` might fall under the control character part of "any-Unicode-character-except-"-or-\-or-control-character". Keep in mind that by the time `JSON.parse` sees that `\n` it will have been converted to a single LF by JavaScript. – mu is too short Aug 28 '13 at 05:21

1 Answers1

3

You will need to escape the backslash in the newline with another backslash

var foo = '{  "success": false,  "errtype": "barf",  "message": "my message\\n"}';

see How do I handle newlines in JSON?

Community
  • 1
  • 1
gherkins
  • 14,603
  • 6
  • 44
  • 70