{}JSON FYI

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.

·5 min read

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)

Frequently asked questions

Are duplicate keys a syntax error?+

No. RFC 8259 calls the behavior 'undefined', not 'invalid'. Most parsers accept duplicates without error. That's exactly why they're dangerous: they silently corrupt your data.

Which value does the parser keep?+

Almost every mainstream parser silently keeps the last value. But it's implementation-defined — don't rely on it.

How do duplicates end up in production JSON?+

Template systems that append keys without checking, code that merges two objects with overlapping keys, or hand-edited JSON where a key was accidentally copy-pasted. Generated JSON is the most common source.

Related tools & guides