in JSON, Why is each name quoted?

Viewed 25525

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?

7 Answers

I leave a quote from a presentation that Douglas Crockford (the creator of the JSON standard) gave to Yahoo.

He talks about how he discovered JSON, and amongst other things why he decided to use quoted keys:

.... That was when we discovered the unquoted name problem. It turns out ECMA Script 3 has a whack reserved word policy. Reserved words must be quoted in the key position, which is really a nuisance. When I got around to formulizing this into a standard, I didn't want to have to put all of the reserved words in the standard, because it would look really stupid.

At the time, I was trying to convince people: yeah, you can write applications in JavaScript, it's actually going to work and it's a good language. I didn't want to say, then, at the same time: and look at this really stupid thing they did! So I decided, instead, let's just quote the keys.
That way, we don't have to tell anybody about how whack it is.

That's why, to this day, keys are quoted in JSON.

You can find the complete video and transcript here.

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

The design philosophy of JSON is "Keep it simple"

"Quote names with "" is a lot simpler than "You may quote names with " or ' but you don't have to, unless they contain certain characters (or combinations of characters that would make it a keyword) and ' or " may need to be quoted depending on what delimiter you selected".

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

No. In JavaScript they are identical.

Both : and whitespace are permitted in identifiers. Without the quotes, this would cause ambiguity when trying to determine what exactly constitutes the identifier.

In javascript objects can be used like a hash/hashtable with key pairs.

However if your key has characters that javascript could not tokenize as a name, it would fail when trying it access like a property on an object rather than a key.

var test  = {};
test["key"] = 1;
test["#my-div"] = "<div> stuff </div>";

// test = { "key": 1, "#my-div": "<div> stuff </div>" };

console.log(test.key);           // should be 1
console.log(test["key"]);        // should be 1
console.log(test["#my-div"]);    // should be "<div> stuff </div>";
console.log(test.#my-div);       // would not work.

identifiers can sometimes have characters that can not be evaluated as a token/identifier in javascript, thus its best to put all identifiers in strings for consistency.

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.

It may reduce data size if quotes on name are only allowed when necessary

Related