Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

in JSON, Why is each name quoted?

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?

like image 594
Cheeso Avatar asked Jan 14 '10 22:01

Cheeso


People also ask

Do JSON field names need quotes?

Properties in JSON have to be strings, and in JSON as string is defined as "a sequence of zero or more Unicode characters, wrapped in double quotes, using backslash escapes". In other words using single quotes, or no quotes at all is not allowed.

Why are JSON keys quoted?

Yes, you need quotation marks. This is to make it simpler and to avoid having to have another escape method for javascript reserved keywords, ie {for:"foo"} .

Is JSON always double quotes?

JSON names require double quotes.

Which is the correct format of writing JSON name?

Data is represented in name/value pairs. Curly braces hold objects and each name is followed by ':'(colon), the name/value pairs are separated by , (comma).


1 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.

like image 175
Christian C. Salvadó Avatar answered Oct 06 '22 00:10

Christian C. Salvadó