Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do JSON keys need to be unique? [duplicate]

The following question is related to a question that I had asked earlier: Help parsing simple JSON (using JSON for JAVA ME)

Do JSON keys need to be unique? For example, I was having trouble parsing the following XML (with JSON ME):

{   "name" : "JACK",   "name" : "JILL",   "name" : "JOHN",   "name" : "JENNY",   "name" : "JAMES",   "name" : "JIM" } 

And, apparently, its because the keys must be unique. I'm just wondering if thats true in all cases or not. For example, if I were using something other than JSON ME, would I be able to parse all of these names?

Thanks.

like image 271
littleK Avatar asked Mar 15 '11 02:03

littleK


People also ask

Can JSON have 2 same keys?

There is no "error" if you use more than one key with the same name, but in JSON, the last key with the same name is the one that is going to be used. In your case, the key "name" would be better to contain an array as it's value, instead of having a number of keys "name".

Do JSON keys need double quotes?

JSON stands for JavaScript Object Notation. It is a lightweight data-interchange format and fully described on www.json.org. JSON is based on JavaScript but the format is stricter. JSON requires double quotes around keys whereas JavaScript does not.

Can JSON have duplicate keys Python?

python - json. loads allows duplicate keys in a dictionary, overwriting the first value - Stack Overflow.


2 Answers

There is no "error" if you use more than one key with the same name, but in JSON, the last key with the same name is the one that is going to be used.

In your case, the key "name" would be better to contain an array as it's value, instead of having a number of keys "name". It doesn't make much sense the same object or "thing" to have two names, or two of the same properties that are in conflict.

E.g.:

{   "name" : [ "JOHN", "JACK", "...", ... ] } 
like image 157
JeanK Avatar answered Oct 04 '22 04:10

JeanK


From RFC 4627:

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. A single colon comes after each name, separating the name from the value. A single comma separates a value from a following name. The names within an object SHOULD be unique.

like image 26
jhanschoo Avatar answered Oct 04 '22 04:10

jhanschoo