Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any practical reason to use quoted strings for JSON keys?

According to Crockford's json.org, a JSON object is made up of members, which is made up of pairs.

Every pair is made of a string and a value, with a string being defined as:

A string is a sequence of zero or more Unicode characters, wrapped in double quotes, using backslash escapes. A character is represented as a single character string. A string is very much like a C or Java string.

But in practice most programmers don't even know that a JSON key should be surrounded by double quotes, because most browsers don't require the use of double quotes.

Does it make any sense to bother surrounding your JSON in double quotes?

Valid Example:

{   "keyName" : 34 } 

As opposed to the invalid:

{    keyName : 34 } 
like image 232
Mark Rogers Avatar asked Nov 17 '10 04:11

Mark Rogers


People also ask

Should JSON keys be quoted?

JavaScript object literals do not require quotes around a key name if the key is a valid identifier and not a reserved word. However, JSON always requires quotes around key names.

Should JSON keys be strings?

In JSON, the “keys” must always be strings. Each of these pairs is conventionally referred to as a “property”. In Python, "objects" are analogous to the dict type. An important difference, however, is that while Python dictionaries may use anything hashable as a key, in JSON all the keys must be strings.

Do JSON numbers need quotes?

In JSON, 6 is the number six. "6" is a string containing the digit 6 . So the answer to the question "Can json numbers be quoted?" is basically "no," because if you put them in quotes, they're not numbers anymore.

Do JSON keys need double quotes?

→ JSON is basically the “String” version of JavaScript Object. Its simply a text-based data format and single quote is not allowed for JSON.


2 Answers

The real reason about why JSON keys should be in quotes, relies in the semantics of Identifiers of ECMAScript 3.

Reserved words cannot be used as property names in Object Literals without quotes, for example:

({function: 0}) // SyntaxError ({if: 0}) // SyntaxError ({true: 0}) // SyntaxError // etc... 

While if you use quotes the property names are valid:

({"function": 0}) // Ok ({"if": 0}) // Ok ({"true": 0}) // Ok 

The own Crockford explains it in this talk, they wanted to keep the JSON standard simple, and they wouldn't like to have all those semantic restrictions on it:

....

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.

...

The ECMAScript 5th Edition Standard fixes this, now in an ES5 implementation, even reserved words can be used without quotes, in both, Object literals and member access (obj.function Ok in ES5).

Just for the record, this standard is being implemented these days by software vendors, you can see what browsers include this feature on this compatibility table (see Reserved words as property names)

like image 197
Christian C. Salvadó Avatar answered Sep 22 '22 19:09

Christian C. Salvadó


Yes, it's invalid JSON and will be rejected otherwise in many cases, for example jQuery 1.4+ has a check that makes unquoted JSON silently fail. Why not be compliant?

Let's take another example:

{ myKey: "value" } { my-Key: "value" } { my-Key[]: "value" } 

...all of these would be valid with quotes, why not be consistent and use them in all cases, eliminating the possibility of a problem?

One more common example in the web developer world: There are thousands of examples of invalid HTML that renders in most browsers...does that make it any less painful to debug or maintain? Not at all, quite the opposite.

Also @Matthew makes the best point of all in comments below, this already fails, unquoted keys will throw a syntax error with JSON.parse() in all major browsers (and any others that implement it correctly), you can test it here.

like image 36
Nick Craver Avatar answered Sep 25 '22 19:09

Nick Craver