I spent an embarrassing afternoon chasing a permissions bug that turned out to be four characters. A config merge had left "role": "admin" and, eleven lines later in the same object, "role": "viewer". The parser picked one. The app deployed the other expectation. Nobody got an error, because a duplicate key is not a parse error. It parses perfectly. It just keeps one value and throws the other away.
When JSON duplicate keys appear, which value is used?
The last one, and nothing in the output tells you the first one is gone. In JavaScript:
JSON.parse('{"id": 1, "id": 2}')
// { id: 2 } (no warning)
In Python:
json.loads('{"id": 1, "id": 2}')
# {'id': 2} (no warning)
Same story in jq, which keeps the last value silently, and in Java's Jackson, where the default is last-wins and configurable. Every mainstream parser does this, because parsers build a map and later assignments overwrite earlier ones. The spec, meanwhile, shrugs: RFC 8259 section 4 says "the names within an object SHOULD be unique." In RFC language, SHOULD is a strong recommendation, not a requirement, and the spec never defines which duplicate wins. So the grammar accepts {"a": 1, "a": 2} as well-formed, parsers invented their own resolution, and they all converged on last-wins by habit. Other formats are stricter: TOML forbids redefining a key outright, and YAML 1.2 treats duplicate mapping keys as an error. JSON is the permissive outlier, which is worth remembering when a pipeline crosses format boundaries.
So where do duplicates actually come from? In my experience it is never intentional. Someone hand-merges two config files. Two microservices both emit a timestamp field and a log pipeline concatenates the objects. Two JSON snippets get pasted together. Increasingly, an LLM generates a long JSON document and regenerates a key halfway through with a different value, because it lost track of its own structure. Every one of these fails silently at parse time and surfaces later as wrong data, which is the expensive kind of bug.
Why a reviver cannot save you
In JavaScript, a JSON.parse reviver function looks like the fix: inspect each key as it arrives, flag the repeat. It runs too late. The reviver executes after the parser has already collapsed the object, so it only ever sees the survivor. By the time your function runs, the first "id" is gone. To detect duplicates you have to look at the raw text before parsing, with a streaming parser like json-source-map or lossless-json, or with a lint pass over the source.
Python at least gives you a hook into that raw layer. json.loads accepts an object_pairs_hook that receives the list of key-value pairs before they collapse into a dict. This is the copy-paste version:
import json
def no_duplicates(pairs):
seen = {}
for key, value in pairs:
if key in seen:
raise ValueError(f"Duplicate key: {key!r}")
seen[key] = value
return seen
json.loads('{"id": 1, "id": 2}', object_pairs_hook=no_duplicates)
# ValueError: Duplicate key: 'id'
It fires for every object in the document, nested ones included, which is exactly where hand-written dupes like to hide.
The decision rule
My rule is split by who produced the payload. If you generate the JSON yourself, duplicate keys are a build problem: add a lint step that rejects them and never think about it again. If you are ingesting someone else's payload, a duplicate key is a defect in their pipeline. Push it back upstream instead of writing clever code that picks a winner, because today "last wins" and tomorrow a new parser, a canonicalizer, or a signature scheme gets a vote and your assumption breaks. Canonical forms like RFC 8785 JCS leave duplicate handling as undefined behavior for exactly this reason, and security reviewers have flagged last-wins resolution in JWTs as exploitable when two systems disagree on which value applies.
The honest position: JSON lets you write something it cannot faithfully represent. A map has one slot per key. If your data needs two values for one name, that is an array, not an object, and the sooner you write it as one, the sooner the quiet data loss stops.
Paste your suspect payload and get clean, valid JSON back, with every fix explained in plain English.
Fix My JSON NowFrequently asked questions
Are duplicate keys valid in JSON?
The grammar accepts them. RFC 8259 says the names within an object SHOULD be unique, which in RFC language is a strong recommendation rather than a hard rule. Most parsers accept duplicate keys without error, which is what makes them dangerous rather than merely invalid.
Which value is used when JSON keys repeat?
In virtually every mainstream parser, the last value wins. JavaScript's JSON.parse, Python's json.loads, jq, and Java's Jackson (by default) all keep the final occurrence and silently discard the earlier ones. It is convention, not law: nothing in the spec guarantees it.
Can a JSON.parse reviver detect duplicate keys?
No. The reviver runs after the parser has already collapsed the duplicates, so it only ever sees the surviving value. To catch duplicates you must inspect the raw text before parsing, using a streaming parser or a lint pass.
How do I reject duplicate keys in Python?
Pass an object_pairs_hook to json.loads that receives the raw key-value pairs before they collapse into a dict, and raise an error when a key repeats. This fires for every object in the document, including nested ones.
Where do duplicate keys usually come from?
Hand-merged config files, log lines concatenated from two services that emit the same field name, copy-pasted JSON snippets, and LLM-generated output that regenerates a key mid-document. Any pipeline that builds JSON by string concatenation is a candidate.
Related reading: Why Is My JSON Invalid? The 7 Syntax Errors That Break JSON.parse · Unexpected Token 'u' in JSON at Position 0: You Parsed the Word "undefined" · JSON with Comments (JSONC): What It Is and How to Handle It
Get one practical JSON debugging note a week: subscribe on Substack.