Alex Rivera | Logout

in JSON, Why is each name quoted?

Asked 2010-01-14T22:18:19.707
96

The JSON spec says that JSON is an object or an array. In the case of an object,

An object structure is represented as a pair of curly brackets surrounding zero or more name/value pairs (or members). A name is a string. ...

And later, the spec says that a string is surrounded in quotes.

Why?

Thus,

{"Property1":"Value1","Property2":18}

and not

{Property1:"Value1",Property2:18}

Question 1: why not allow the name in the name/value pairs to be unquoted identifiers?


Question 2: Is there a semantic difference between the two representations above, when evaluated in Javascript?

Edit
Report

1 Answer

-4

If json describes objects, then in practise you get the following

var foo = {};

var bar = 1;

foo["bar"] = "hello";
foo[bar] = "goodbye";

so then,

foo.bar == "hello";
foo[1] == "goodbye" // in setting it used the value of var bar

so even if your examples do produce the same result, their equivalents in "raw code" wouldn't. Maybe that's why?? dunno, just an idea.

answered 2010-01-14T22:42:53.703

Your Answer