I started getting errors in my browser, parsing JSON return data from the server. At first I thought it is something specific to my data, but even something silly like
{"a": 1}
results in the error “invalid label” in Firefox and “SyntaxError: Unexpected token :” in Chrome. When changing it to
{a:1}
(i.e. removing the double quote around the label) it works fine. However, I am sure that even with the double quotes, it is valid JSON (and JSONLint agrees with this).
The workaround I have seen is to surround the JSON object with brackets like so:
({"a":1})
and this works from the browser console, but not when I change my server data (which is responding to a JSONP call) and I can’t change it in the browser either, because I am using a library (Dojo) and I don’t have the opportunity to intercept the return data before it is parsed.
UPDATE
I found a bug in my server code where I am not wrapping the data in a JS function for the JSONP response. This has fixed the problem, and I subsequently need to delete this question. Apologies, and thanks for the responses though.
standing alone is not json, it’s a block.
Where
"a":would be a jump label for continue. And such labels are written without".Whereas
({"a":1})is an expression. And as such{"a": 1}will get parsed as json.