views:

115

answers:

2

Anybody out there notice anything wrong with this JSON? It validates at JSONLint.com, but neither Chrome nor Firefox's native JSON parse functions will properly parse it.

Any ideas?

{
        "result": "{\"players\":[{\"name\":\"User 522\",\"turn\":true,\"score\":0},{\"name\":\"User 925\",\"turn\":false,\"score\":5}],\"enableControls\":false}",
        "error": "null",
        "id": "7"
}
+1  A: 
Sky Sanders
should be a comment. JSON is a string representation of a JavaScript object, it's rather common for questioners to post without the string delimiters. It doesn't make the question any less valid.
Andy E
@Andy - thanks for that. :-\ I was in the process of explaining. And whether or not it is common to mis represent a data structure does not make it inappropriate to point out the difference.
Sky Sanders
@Sky: you expanded your answer, so downvote removed (as per my downvote removal policy, paragraph 23 line 7 ;-)). Indeed, it's not inappropriate to point out the difference, but it is more appropriate to use comments for such a thing :-)
Andy E
"That is not JSON" is wrong as it is JSON. It's perfectly in keeping with the JSON specs which even say to escape double quotes in strings
Bob
@bob - you are right, let me be more precise.
Sky Sanders
@Bob: actually, he's right. JSON should be a string, and without the curly braces wrapped with string delimiters it's just a plain JavaScript object literal.
Andy E
The poster just didn't show the string part, which is fine. If you're going to show pure JSON it's silly to show it encased in quotes as that forces it into the context of some language. In the OP comments it says this JSON is coming from a server, so is likely a string
Bob
It's coming from the server as a string.
Ronald
@Ronald, if the server is returning that exact string to you, the server is faulty. There is no practical way to parse that string. I suspect that there is more to this.
Sky Sanders
+3  A: 

Even though you json looks kind of weird, it conforms with the json specification.

You problem comes from an escaping problem when defining literals in Firefox or Chrome. The "\" (backslash) character needs to be escaped with a backslash.

Example 1:

JSON.parse('{"key":"\""}'); breaks

Example 2:

JSON.parse('{"key":"\\""}'); works

So JSONLint.com is right, and Firefox is right and Chrome is right also.

You would not hit this problem if you were testing through an ajax request, because the escaping would be handled automatically. You are hitting the problem because you want to feed the json string as a literal (hence the need for escaping)

I hope this will help you.

Jerome WAGNER
@Jerome WAGNER: I think the \ is only in there to escape the quotes.
R0MANARMY
The json standard imposes double quotes around values. So if you want a double quote in a json string value, you need to escape it with a backslash. This is for the json side of the problem. Now when you input a literal single-quoted string into firefox, backslash needs to be escaped with a backslash. Who doesn't love escaping ?
Jerome WAGNER
Curious: '{"result":"{\"players\":[{\"name\":\"User 522\",\"turn\":true,\"score\":0},{\"name\":\"User 925\",\"turn\":false,\"score\":5}],\"enableControls\":false}","error":"null","id":"7"}'.replace('\','');Produces "SyntaxError: Unexpected token ILLEGAL" The same error I get when I try to use JSON.parse
Ronald