Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why JSON allows only string to be a key?

Why does JSON only allow a string to be a key of a pair? Why not other types such as null, number, bool, object, array? Considering JSON is tightly related with JavaScript, could I conclude the reason from JavaScript specification (ECMA-262)? I'm totally a newbie to JavaScript, could you help me to point it out.

like image 207
pipipi Avatar asked Feb 16 '12 02:02

pipipi


People also ask

Is the key in JSON always a string?

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.

Can JSON keys be numbers?

JSON only allows key names to be strings. Those strings can consist of numerical values.

Can JSON have only keys?

What JSON looks like. JSON doesn't have to have only key:value pairs; the specification allows to any value to be passed without a key. However, almost all of the JSON objects that you see will contain key:value pairs.

Can JSON just be a string?

JSON can actually take the form of any data type that is valid for inclusion inside JSON, not just arrays or objects. So for example, a single string or number would be valid JSON.


2 Answers

The JSON format is deliberately based on a subset of JavaScript object literal syntax and array literal syntax, and JavaScript objects can only have strings as keys - thus JSON keys are strings too. (OK, you can sort of use numbers as JavaScript object keys, but really they get converted to strings.)

Note that the point of JSON is that it is a string representation of data to allow easy exchange between programs written in different languages running on different machines in different environments. If you wanted to use an object as a key then that object would in turn have to be somehow represented as a string for transmission, but then the receiving language would need to be able to use objects as keys and that would mean you'd need a limited subset of JSON for those languages which would just be a mess.

"Considering JSON is a part of JavaScript"

No, it isn't. Newer browsers provide methods for creating and parsing JSON, but they're not part of the language as such except that JSON is a string format and JavaScript can do strings. JSON is always a string representation - it has to be parsed to create an object for use within JavaScript (or other languages) and once that happens JavaScript (or the other languages) treat the resulting object the same as any other object.

(Note also that a particular bit of JSON doesn't necessarily have any keys at all: it could just be an array, like '["one","two","three"]'.)

like image 196
nnnnnn Avatar answered Oct 17 '22 07:10

nnnnnn


Main reason according to the discoverer of JSON representation is,

while parsing JSON data, there is a chance/possibility that, key you are using to refer a value might be a reserved word in your parsing language.

Refer this talk by Douglas Crockford, who is the discoverer of JSON representation.

Example :

{ 
    id: 1234, 
    name: "foo", 
    do: "somthing"
} 

Since JSON is a cross language compatibility, We can use this data set in many languages. But, the word do is a keyword in Javascript. It will end up in syntax error while parsing.

like image 37
Aparichith Avatar answered Oct 17 '22 07:10

Aparichith