Skip to content
Back to Blog
By JSONConvert Team··5 min read

Duplicate Keys in JSON: Which Value Wins?

Paste {"role": "user", "role": "admin"} into any JSON validator and it passes. Duplicate keys in JSON are not a syntax error, but the spec never says which value a parser should keep. Most parsers keep the last one, some keep the first, and a few reject the document. When two services read the same payload with different parsers, that gap turns into a real bug.

Does JSON allow duplicate keys?

Technically, yes. RFC 8259 says the names within an object "SHOULD be unique", which is a recommendation, not a rule. It also warns that when names are not unique, the behavior of software that receives the object is unpredictable. ECMA-404 says nothing about uniqueness at all.

The stricter profile is I-JSON (RFC 7493), which says objects MUST NOT have duplicate names. Very few parsers enforce I-JSON by default, so in practice you get whatever your library decided.

That is also why a syntax check won't help you here. The JSON validator answers "is this valid JSON?", and a duplicate key is valid JSON. It passes silently.

What each parser does

Here is the same input run through common parsers:

ParserDefault resultStrict option
JavaScript JSON.parse{ role: 'admin' } (last wins)None built in
Python json.loads{'role': 'admin'} (last wins)object_pairs_hook
jq 1.7{"role":"admin"} (last wins)None
Go encoding/jsonlast winsencoding/json/v2 rejects by default
Rust serde_json into a structerror: duplicate field roleDefault
Rust serde_json::Valuelast winsDeserialize into a struct
Java Jacksonlast winsSTRICT_DUPLICATE_DETECTION
PostgreSQL jsonblast winsUse json to keep both
The JavaScript, Python and jq rows were run on Node 26, Python 3.14 and jq 1.7.1. "Last wins" is the common answer, but it is a convention, not a guarantee. Older Erlang and some C parsers keep the first value, and that difference is where the trouble starts.

Why it is a security bug, not a curiosity

Duplicate keys become dangerous when two components disagree on which value is real. The textbook case is CVE-2017-12635 in Apache CouchDB. A user could create an account with this body:

CouchDB validated documents in JavaScript, which saw the last roles value, an empty array, and allowed it. The Erlang side that stored and enforced permissions kept the first value, ["_admin"]. The attacker walked in as an admin.

The same pattern shows up anywhere a gateway, WAF or validator parses JSON with one library and the service behind it uses another. If you sign a payload, check it for policy, or log it in one language and act on it in another, duplicate keys let an attacker show each side a different document.

Go has an extra twist: case-insensitive matching

Go's original encoding/json matches object keys to struct fields case-insensitively. These two keys are different strings, but both land in the same field:

A JavaScript service sees two separate keys and reads role as "user". Your Go service reads "admin". No validator will flag it because nothing is technically duplicated.

Go 1.25 shipped encoding/json/v2 behind GOEXPERIMENT=jsonv2. It matches names case-sensitively and rejects duplicate names by default, which closes both holes. If you generate structs with the JSON to Go converter, the tags it writes are exact names, so the struct is ready for v2's case-sensitive matching.

How to detect duplicate keys

Since parsers drop the evidence, you have to catch duplicates while parsing, not after.

In Python, object_pairs_hook receives every key-value pair before they become a dict:

The hook runs for every nested object, so one function covers the whole document. This is also a handy one-off checker for files you don't trust.

In Rust, deserialize into a struct rather than Value and serde does the check for you. In Java, enable it on the parser:

JavaScript is the awkward one. JSON.parse has no hook that sees raw pairs, and a reviver function only runs after duplicates are already collapsed. If you need this in Node, use a streaming tokenizer that emits keys as it reads them, or run the check in the service that receives the request.

What JSON Schema can and can't do

JSON Schema validates the parsed value, so by the time a schema runs the duplicate is gone. A schema can still help in a narrower way. Generate one from a known-good payload with the JSON Schema generator, then add "additionalProperties": false to each object. That rejects unexpected keys, which catches the Go case-mismatch trick when Role and role are both sent to a strict validator. It won't catch role twice.

A quick way to see what your code will see

When you are debugging a payload by hand, run it through the JSON formatter. It uses JSON.parse, so the output is exactly what a JavaScript consumer gets. If a value you expected has vanished, or an object has fewer keys than the raw text, you have a duplicate. Compare the line count of the raw object to the formatted one and the difference points straight at it.

The fix is almost never to pick a winner. Reject the document at the edge, in the first service that parses it, and return a 400. Anything downstream should never have to guess which role you meant.

Related Tools