I’m getting errors, in both chrome and firefox developer tools, when trying to evaluate the following:
{
"a": "",
"b": ""
}
jsonlint.com tells me it’s valid. Putting this code in an actual javascript file and running it works fine. The strangeness shows up only when I run this in the console in chrome developer tools or firebug. What’s going on here?
You can’t execute JSON in the console. The JavaScript engine thinks its a block statement, with a label.
So this:
is interpreted as a block statement.
TheNext, the"a":part is interpreted as a label. The"", "b"part is interpreted as an expression (two string literals and a comma operator in-between). Now the second
:character is invalid in that position…"a"is interpreted as a string literal, and the:is not valid at that position.You work with JSON like so:
.jsonfile,JSON.parse().(You can also keep JSON data as a string in a variable, for instance, or in the
localStorageobject. Either way, in regard to JavaScript, JSON data should always come as a string value.)