285

Is this valid json?

{
    "a" : "x",
    "a" : "y"
}

http://jsonlint.com/ says yes.

http://www.json.org/ doesn't say anything about it being forbidden.

But obviously it doesn't make much sense, does it? Most implementations probably use a hashtable so it is being overriden anyways.

vallentin
  • 23,478
  • 6
  • 59
  • 81
clamp
  • 33,000
  • 75
  • 203
  • 299
  • 1
    C# 's Json.NET removes the first key pair if you deserialise to a `Dictionary` – Sam Leach Feb 17 '14 at 15:20
  • 1
    In case anyone arrives here hoping for a solution to find duplicate values in JSON strings, check out the [free online json validator](http://www.freeformatter.com/json-validator.html) – Pepijn Olivier May 11 '15 at 10:37
  • 1
    *http://jsonlint.com/ says yes.* it does not, it removes all but the last key-value pair and then validates it, which makes it valid – Tim Sep 09 '15 at 12:58
  • Yes, it's valid semantically according to the stanard. But as you say, this will likely break so many places, no-one should use it this way. – Herbert Van-Vliet Nov 21 '15 at 09:27
  • 14
    Then the standard is broken – Bradley Thomas Dec 18 '15 at 22:22
  • If you try to access `key["a"]` in JavaScript, the last value in the set: `y` will be returned. – gatzkerob Oct 01 '17 at 23:37
  • 2
    I used the key name "--" as a commentor and the value is a single string line as comment. So i hope no parser will complain about it. – Lothar Sep 12 '18 at 12:34
  • jsonlint.com now says the JSON is invalid: "SyntaxError: Duplicate key 'a' on line 3" – mbonness Oct 04 '19 at 18:53
  • When serializing dictionary data structures that allow duplicate keys, one way to avoid this problem is to serialize it as an array of arrays, where each sub-array is two elements containing the key and the value. E.g.: `[["a","x"],["a","y"]]` – Emile Cormier Feb 27 '21 at 00:24

12 Answers12

198

The short answer: Yes but is not recommended.
The long answer: It depends on what you call valid...

ECMA-404 "The JSON Data Interchange Syntax" doesn't say anything about duplicated names (keys).

However, RFC 8259 "The JavaScript Object Notation (JSON) Data Interchange Format" says:

The names within an object SHOULD be unique.

In this context SHOULD must be understood as specified in BCP 14:

SHOULD This word, or the adjective "RECOMMENDED", mean that there may exist valid reasons in particular circumstances to ignore a particular item, but the full implications must be understood and carefully weighed before choosing a different course.

RFC 8259 explains why unique names (keys) are good:

An object whose names are all unique is interoperable in the sense that all software implementations receiving that object will agree on the name-value mappings. When the names within an object are not unique, the behavior of software that receives such an object is unpredictable. Many implementations report the last name/value pair only. Other implementations report an error or fail to parse the object, and some implementations report all of the name/value pairs, including duplicates.


Also, as Serguei pointed out in the comments: ECMA-262 "ECMAScript® Language Specification", reads:

In the case where there are duplicate name Strings within an object, lexically preceding values for the same key shall be overwritten.

In other words, last-value-wins.


Trying to parse a string with duplicated names with the Java implementation by Douglas Crockford (the creator of JSON) results in an exception:

org.json.JSONException: Duplicate key "status"  at
org.json.JSONObject.putOnce(JSONObject.java:1076)
Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614
user454322
  • 7,300
  • 5
  • 41
  • 52
  • 2
    JSON is supposed to be valid javascript, so it's relevant to check whether duplicate keys are valid JS in literals. V8 seems to accept them: `d8 -e 'x={"a":1,"a":2}; print(x.a);'` This prints 2. –  Jun 10 '16 at 21:27
  • 11
    Also the [ECMA-262](http://www.ecma-international.org/ecma-262/6.0/#sec-internalizejsonproperty) spec for `JSON.parse()` explicitly says `In the case where there are duplicate name Strings within an object, lexically preceding values for the same key shall be overwritten.` (in other words last-value-wins). – Serguei Jun 17 '16 at 17:38
  • 6
    @BenCrowell: As far as I know, JSON is not supposed to be valid JavaScript and there are cases where it's not, see http://timelessrepo.com/json-isnt-a-javascript-subset. With that said, it was of course heavily inspired by JavaScript (it even says so in the JSON specification). – Simon Touchtech Aug 17 '16 at 13:03
  • 3
    It's worth noting that when using javascript "strict mode", if there are two identical keys chrome will use the second key-value pair and ignore the first. IE11 will throw an exception. – Shahar Sep 13 '17 at 11:23
153

From the standard (p. ii):

It is expected that other standards will refer to this one, strictly adhering to the JSON text format, while imposing restrictions on various encoding details. Such standards may require specific behaviours. JSON itself specifies no behaviour.

Further down in the standard (p. 2), the specification for a JSON object:

An object structure is represented as a pair of curly bracket tokens surrounding zero or more name/value pairs. A name is a string. A single colon token follows each name, separating the name from the value. A single comma token separates a value from a following name.

Diagram for JSON Object

It does not make any mention of duplicate keys being invalid or valid, so according to the specification I would safely assume that means they are allowed.

That most implementations of JSON libraries do not accept duplicate keys does not conflict with the standard, because of the first quote.

Here are two examples related to the C++ standard library. When deserializing some JSON object into a std::map it would make sense to refuse duplicate keys. But when deserializing some JSON object into a std::multimap it would make sense to accept duplicate keys as normal.

Timothy Shields
  • 75,459
  • 18
  • 120
  • 173
  • 6
    i guess i can accept this as an answer, though i like the mention of @PatrickGoley that on json.org it is called a set of key/value-pairs which implies uniqueness which would mean it is not valid. – clamp Feb 17 '14 at 16:02
  • 8
    @clamp json.org is not the standard and, as far I can tell, is not run by Emca International. json.org seems presented anonymously. This is **the** specification: http://www.ecma-international.org/publications/files/ECMA-ST/ECMA-404.pdf What it says on json.org is not relevant. – Timothy Shields Feb 17 '14 at 16:05
  • 5
    @clamp Consider the `std::multimap` example I just added. It can be serialized as a JSON object with potentially duplicate keys. – Timothy Shields Feb 17 '14 at 16:16
  • 2
    @clamp being a set of key/value pairs doesn't preclude duplicate names. `{"a":1,"a":2}` is a set of two distinct key/value pairs. Indeed, even `{"a":1,"a":1}` could be thought of as a set of key/value pairs that happens to have just one element. The fact that it is repeated can be thought of as just a syntactic quirk. A better definition would be, "An object is a partial function from strings (names) to values." – Marcelo Cantos Dec 02 '18 at 03:19
  • 7
    @TimothyShields And that standard you linked to says, "The JSON syntax does not impose any restrictions on the strings used as names, does not require that name strings be unique, and does not assign any significance to the ordering of name/value pairs" – Charles Jul 16 '19 at 20:37
  • @Charles comment is correct, and the answer should be updated to reflect that. – Martin Thøgersen Aug 22 '20 at 13:01
  • @MartinThøgersen Can you suggest an edit? The answer does not seem to contradict that comment. – Charles Aug 24 '20 at 13:57
20

There are 2 documents specifying the JSON format:

  1. http://json.org/
  2. https://www.rfc-editor.org/rfc/rfc7159

The accepted answer quotes from the 1st document. I think the 1st document is more clear, but the 2nd contains more detail.

The 2nd document says:

  1. Objects

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.

So it is not forbidden to have a duplicate name, but it is discouraged.

Community
  • 1
  • 1
toongeorges
  • 1,844
  • 17
  • 14
14

I came across a similar question when dealing with an API that accepts both XML and JSON, but doesn't document how it would handle what you'd expect to be duplicate keys in the JSON accepted.

The following is a valid XML representation of your sample JSON:

<object>
  <a>x</a>
  <a>y</a>
</object>

When this is converted into JSON, you get the following:

{
  "object": {
    "a": [
      "x",
      "y"
    ]
  }
}

A natural mapping from a language that handles what you might call duplicate keys to another, can serve as a potential best practice reference here.

Hope that helps someone!

Darren Shewry
  • 10,179
  • 4
  • 50
  • 46
6

The JSON spec says this:

An object is an unordered set of name/value pairs.

The important part here is "unordered": it implies uniqueness of keys, because the only thing you can use to refer to a specific pair is its key.

In addition, most JSON libs will deserialize JSON objects to hash maps/dictionaries, where keys are guaranteed unique. What happens when you deserialize a JSON object with duplicate keys depends on the library: in most cases, you'll either get an error, or only the last value for each duplicate key will be taken into account.

For example, in Python, json.loads('{"a": 1, "a": 2}') returns {"a": 2}.

Max Noel
  • 8,810
  • 1
  • 27
  • 35
  • 26
    does unordered imply uniqueness? I think set is the operative word here – Patrick Goley Feb 17 '14 at 15:33
  • 8
    An unordered collection of colors: Blue, Green, Green, Blue, Red, Blue, Green - It has duplicates. – Timothy Shields Feb 17 '14 at 15:33
  • 2
    Which spec are you referring to? I don't see that in there ... http://www.ecma-international.org/publications/files/ECMA-ST/ECMA-404.pdf – svidgen Feb 17 '14 at 15:34
  • 5
    The text phrase you are quoting, "An object is an unordered set of name/value pairs," does not appear in the JSON spec... – Timothy Shields Feb 17 '14 at 15:36
  • @PatrickGoley: does "set" imply uniqueness? maybe yes, cause otherwise we wouldnt have a [multiset](http://en.wikipedia.org/wiki/Multiset) – clamp Feb 17 '14 at 15:37
  • 7
    I see now that you were quoting json.org. It's 'close' to official, but it's not the specification. Top of the page has a link to the specification, which is repeated verbatim on json.org. If you search the spec., the word "unordered" does not appear, and the word "set" appears only in contexts unrelated to JSON objects. – Timothy Shields Feb 17 '14 at 15:38
  • @clamp yes, generally a set is defined to have unique members https://en.wikipedia.org/wiki/Set_(computer_science) – Patrick Goley Feb 17 '14 at 15:43
  • 5
    Note that it says "unordered set **of name/value pairs**", pairs not names. That is, `{ (a,b), (a,c) }` _is_ a unique set. So technically under the json.org definition `{"a":1,"a":2}` is valid but `{"a":1,"a":2,"a":1}` is not. Also note that [ECMA-404](http://www.ecma-international.org/publications/files/ECMA-ST/ECMA-404.pdf) (the actual standard) avoids using the word "set": `An object structure is represented as a pair of curly bracket tokens surrounding zero or more name/value pairs.` – Serguei Jun 17 '16 at 18:04
  • I think its worth noting the inconsistency is bidirectional since `json.dumps({1: 1, '1': 2})` is valid in python (number as key), but the generated json string is `'{"1": 1, "1": 2}'`, so python's native `json` library actually **allows** duplicate keys, but the representing data structure doesn't. – Reut Sharabani Apr 20 '17 at 14:21
6

Posting and answer because there is a lot of outdated ideas and confusion about the standards. As of December 2017, there are two competing standards:

RFC 8259 - https://www.rfc-editor.org/rfc/rfc8259

ECMA-404 - http://www.ecma-international.org/publications/files/ECMA-ST/ECMA-404.pdf

json.org suggests ECMA-404 is the standard, but this site does not appear to be an authority. While I think it's fair to consider ECMA the authority, what's important here is, the only difference between the standards (regarding unique keys) is that RFC 8259 says the keys should be unique, and the ECMA-404 says they are not required to be unique.

RFC-8259:

"The names within an object SHOULD be unique."

The word "should" in all caps like that, has a meaning within the RFC world, that is specifically defined in another standard (BCP 14, RFC 2119 - https://www.rfc-editor.org/rfc/rfc2119) as,

  1. SHOULD This word, or the adjective "RECOMMENDED", mean that there may exist valid reasons in particular circumstances to ignore a particular item, but the full implications must be understood and carefully weighed before choosing a different course.

ECMA-404:

"The JSON syntax does not impose any restrictions on the strings used as names, does not require that name strings be unique, and does not assign any significance to the ordering of name/value pairs."

So, no matter how you slice it, it's syntactically valid JSON.

The reason given for the unique key recommendation in RFC 8259 is,

An object whose names are all unique is interoperable in the sense that all software implementations receiving that object will agree on the name-value mappings. When the names within an object are not unique, the behavior of software that receives such an object is unpredictable. Many implementations report the last name/value pair only. Other implementations report an error or fail to parse the object, and some implementations report all of the name/value pairs, including duplicates.

In other words, from the RFC 8259 viewpoint, it's valid but your parser may barf and there's no promise as to which, if any, value will be paired with that key. From the ECMA-404 viewpoint (which I'd personally take as the authority), it's valid, period. To me this means that any parser that refuses to parse it is broken. It should at least parse according to both of these standards. But how it gets turned into your native object of choice is, in any case, unique keys or not, completely dependent on the environment and the situation, and none of that is in the standard to begin with.

Community
  • 1
  • 1
xthr33
  • 71
  • 1
  • 2
  • 1
    json.org actually predates the ECMA standardization. I believe it was actually setup by Crockford himself (which is why it has a shameless plug for his book). At that point it was the authority for JSON. – max Dec 30 '19 at 16:35
5

SHOULD be unique does not mean MUST be unique. However, as stated, some parsers would fail and others would just use the last value parsed. However, if the spec was cleaned up a little to allow for duplicates then I could see a use where you may have an event handler which is transforming the JSON to HTML or some other format... In such cases it would be perfectly valid to parse the JSON and create another document format...

[
  "div":
  {
    "p": "hello",
    "p": "universe"
  },
  "div":
  {
    "h1": "Heading 1",
    "p": "another paragraph"
  }
]

could then easily parse to html for example:

<body>
 <div>
  <p>hello</p>
  <p>universe</p>
 </div>
 <div>
  <h1>Heading 1</h1>
  <p>another paragraph</p>
 </div>
</body>

I can see the reasoning behind the question but as it stands... I wouldn't trust it.

printz
  • 3
  • 3
Colin Saxton
  • 59
  • 1
  • 1
  • 1
    Your first example’s array is missing a comma. Also, it is inconsistent with itself. If you’re going to use a dictionary as an ordered collection, your outer array should also just be an object. I.e., `{"div":{"p":"hello","p":"universe"}, "div":{"h1":"Heading 1","p":"another paragraph"}}`. Now, lots of people and frameworks treat JSON objects as unordered dictionaries, but JavaScript and e.g. MongoDB’s API rely on ordering of keys within dictionaries, so what you’re suggesting (ordered dictionaries) is not unheard of. You’d just need a specialized parser. – binki Jul 06 '17 at 16:35
  • Ordered dictionaries still have unique keys. – Tomasz Gandor Mar 16 '21 at 10:21
  • the array can have associated values? – Anton Krug May 04 '21 at 19:27
2

It's not defined in the ECMA JSON standard. And generally speaking, a lack of definition in a standard means, "Don't count on this working the same way everywhere."

If you're a gambler, "many" JSON engines will allow duplication and simply use the last-specified value. This:

var o = {"a": 1, "b": 2, "a": 3}

Becomes this:

Object {a: 3, b: 2}

But if you're not a gambler, don't count on it!

svidgen
  • 13,744
  • 4
  • 33
  • 58
2

Asking for purpose, there are different answers:

Using JSON to serialize objects (JavaScriptObjectNotation), each dictionary element maps to an indivual object property, so different entries defining a value for the same property has no meaning.

However, I came over the same question from a very specific use case: Writing JSON samples for API testing, I was wondering how to add comments into our JSON file without breaking the usability. The JSON spec does not know comments, so I came up with a very simple approach:

To use duplicate keys to comment our JSON samples. Example:

{ "property1" : "value1", "REMARK" : "... prop1 controls ...", "property2" : "value2", "REMARK" : "... value2 raises an exception ...", }

The JSON serializers which we are using have no problems with these "REMARK" duplicates and our application code simply ignores this little overhead.

So, even though there is no meaning on the application layer, these duplicates for us provide a valuable workaround to add comments to our testing samples without breaking the usability of the JSON.

aknoepfel
  • 81
  • 5
  • This is a bad idea. By including duplicate keys, even if you don't need to read the data in them, you're relying on undefined behaviour. Some parsers, such as Crockford's JSON-java parser, throw an exception and refuse to parse the data. – Richard Smith Mar 28 '16 at 14:13
  • Actually working perfectly in our environment, so serving my needs, even though I agree with you that it's some kind out of spec ;) – aknoepfel Jul 22 '16 at 16:14
  • @RichardSmith I’d say the parsers and newer ES specs have defined the behavior. – binki Jul 06 '17 at 16:36
2

According to RFC-7159, the current standard for JSON published by the Internet Engineering Task Force (IETF), states "The names within an object SHOULD be unique". However, according to RFC-2119 which defines the terminology used in IETF documents, the word "should" in fact means "... there may exist valid reasons in particular circumstances to ignore a particular item, but the full implications must be understood and carefully weighed before choosing a different course." What this essentially means is that while having unique keys is recommended, it is not a must. We can have duplicate keys in a JSON object, and it would still be valid.

From practical application, I have seen the value from the last key is considered when duplicate keys are found in a JSON.

Navaneetha
  • 93
  • 1
  • 7
  • JSON has no comments - the "duplicate" above the final value is the equivalent "commented out". This is one valid reason to use it. – Tomasz Gandor Mar 16 '21 at 10:25
1

The standard does say this:

Programming languages vary widely on whether they support objects, and if so, what characteristics and constraints the objects offer. The models of object systems can be wildly divergent and are continuing to evolve. JSON instead provides a simple notation for expressing collections of name/value pairs. Most programming languages will have some feature for representing such collections, which can go by names like record, struct, dict, map, hash, or object.

The bug is in node.js at least. This code succeeds in node.js.

try {
     var json = {"name":"n","name":"v"};
     console.log(json); // outputs { name: 'v' }
} catch (e) {
     console.log(e);
}
chiwangc
  • 3,566
  • 16
  • 26
  • 32
John Carlson
  • 321
  • 1
  • 13
  • 1
    It's not a bug, and section you quoted explains why it is not: different languages behave differently, and JSON parsers will do what's most natural for that language. In any case, this answer doesn't add anything that user454322 hasn't already said above. – Richard Smith Mar 28 '16 at 14:14
0

In C# if you deserialise to a Dictionary<string, string> it takes the last key value pair:

string json = @"{""a"": ""x"", ""a"": ""y""}";
var d = JsonConvert.DeserializeObject<Dictionary<string, string>>(json);
// { "a" : "y" }

if you try to deserialise to

class Foo
{
    [JsonProperty("a")]
    public string Bar { get; set; }

    [JsonProperty("a")]
    public string Baz { get; set; }
}

var f = JsonConvert.DeserializeObject<Foo>(json);

you get a Newtonsoft.Json.JsonSerializationException exception.

Sam Leach
  • 12,746
  • 9
  • 45
  • 73