Do JSON keys need to be unique? [duplicate]

Solution 1:

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", "...", ... ]
}

Solution 2:

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.

Solution 3:

jQuery is able to parse it. But if you try to access it, it's just giving back the last value.

Check out http://jsfiddle.net/MQmM4/2/

So, it's parsable, I guess, but the value gets overridden if you use the same key.

Solution 4:

here is a possible solution using array. just use array index

the_json_array.getJSONObject(0);


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