Guide
Duplicate keys in JSON: what happens and how to avoid them
Learn why repeated object names produce unpredictable results and how to catch data loss before converting JSON.
by Tools in a Tab · Published on · Reviewed on
Short answer
A duplicate key in a JSON object has no portable outcome. The specification recommends unique names, but parsers may keep the first value, keep the last, preserve every occurrence, or reject the input. The safe response is to detect the repetition and decide what the data should mean before parsing or converting it.
A minimal example
This text follows the JSON grammar but repeats status:
{
"status": "pending",
"status": "shipped"
}
A common parser based on JSON.parse leaves one value and normally keeps
"shipped". That does not make the document unambiguous: another receiver can
behave differently. Once the first value is discarded, formatting the parsed
result cannot recover it.
Section 4 of RFC 8259 says object names should be unique and warns that behavior with repeated names is unpredictable across implementations.
How to fix it
First decide whether the repetition is accidental or represents several real values:
- If only one status is current, keep one property.
- If both values form a list, use an array with a descriptive name.
- If they are historical events, model objects with an order or date rather than repeating a key.
For example, a sequence can be represented unambiguously:
{
"history": [
{ "status": "pending", "order": 1 },
{ "status": "shipped", "order": 2 }
]
}
Check before losing the evidence
Detect duplicates in the original text, before turning it into a language object. Tools in a Tab’s JSON to CSV converter rejects duplicate names before producing rows because a table cannot decide which value belongs to the column.
The JSON validator locates syntax errors, but an input with duplicate keys can be accepted by parsers that follow the grammar. Therefore, “it parses” does not mean “it is interoperable.” Check names before formatting: after a parser reduces them to one value, the original collision may have disappeared.
Practical rule
Require unique names within each object, run a duplicate-name check at the boundary, and do not use ordering to resolve conflicts. If several values are needed, express them explicitly with an array or separate properties. The JSON will then preserve its meaning across languages, APIs, and tools.