Duplicate keys in JSON — what parsers do and how to fix them
RFC 8259 leaves duplicate-key behavior undefined. Here's what real parsers actually do, why it's almost always a bug, and how to find and fix duplicates.
A duplicate key occurs when two or more properties in the same JSON object share the same name:
{
"id": 1,
"name": "Ada",
"id": 2 // duplicate!
}What the spec says
RFC 8259 explicitly leaves duplicate-key behavior undefined: "The names within an object SHOULD be unique." It is not an error in the spec — which is exactly why it's dangerous.
What real parsers actually do
- JSON.parse (V8 / SpiderMonkey) — keeps the last value. Silent.
- Python json.loads — keeps the last value. Silent.
- Go encoding/json — keeps the last value. Silent.
- Java Jackson — configurable; by default keeps the last value.
- jq — keeps the last value. Silent.
None of them error by default, which means duplicate keys cause silent data loss — one value is quietly overwritten.
Why duplicates almost always mean a bug
If both copies of the key are intentional, the right structure is an array. If one is a mistake — a copy-paste error, a template substitution gone wrong, or a merge artifact — one value is dropped without warning. Neither case is what the author wanted.
How to find duplicate keys
The JSON FYI linter flags every duplicate key with the line and column of both the original definition and the duplicate. Paste your JSON below:
Find the problem with the JSON linter →
The linter surfaces trailing commas, duplicate keys, and other common issues with line and column numbers.
Open JSON Linter →How to fix them
- Remove the duplicate if it was added by mistake — keep the intended key and delete the other.
- Merge into an array if both values are valid:
"roles": ["admin", "editor"] - Rename one key if both values belong in the object under different names.
Preventing duplicates in generated JSON
// JavaScript — object spread deduplicates automatically (last value wins)
const merged = { ...defaults, ...overrides };
JSON.stringify(merged); // safe — each key appears once
// Python — dict merge also deduplicates
data = {**defaults, **overrides}
json.dumps(data)