Home → JSON Duplicate Key Detector

JSON Duplicate Key Detector

Detect duplicate keys in JSON objects before they cause bugs.

About This Tool

Detect duplicate keys in JSON objects before they cause bugs. This tool runs entirely in your browser — no data is ever sent to a server. Free to use, no account required.

Why Duplicate JSON Keys Are a Problem

Duplicate keys in a JSON object create ambiguity — different parsers resolve them differently, leading to subtle and hard-to-debug data issues.

Parser Behavior with Duplicates

Different JSON parsers handle duplicates differently: most keep the last value, some keep the first, and strict parsers throw an error. This inconsistency means the same JSON file can produce different data in different languages or libraries.

Silent Data Loss

When a parser silently takes only the last duplicate value, all earlier values are lost without any error or warning. This is especially dangerous in automated pipelines where the data is not manually inspected.

How to Fix Duplicate Keys

After detecting duplicates, you have several options depending on whether you need to preserve both values or just pick one.

Keep First or Last Occurrence

The most common fix — decide which duplicate to keep and remove the others. Keep first preserves the original value; keep last preserves the most recent override.

Rename Duplicate Keys

If both values are needed, rename the keys to make them unique — for example, name and name_2. This avoids data loss while resolving the ambiguity.

Frequently Asked Questions

Are duplicate keys valid in JSON?+
Duplicate keys are technically allowed by the JSON RFC (RFC 8259), which says behavior with duplicate keys is undefined. However, this is practically a bug — most parsers handle duplicates inconsistently. Python and JavaScript's JSON.parse both keep the last value, while some strict parsers raise an error. Treat duplicate keys as a bug and fix them.
How does this tool detect duplicate keys?+
The tool uses a custom parser that tracks every key in every object as it reads through the JSON. Unlike JSON.parse which silently overwrites duplicates, this parser records each occurrence with its line number and value, then reports all keys that appear more than once.
Can duplicate keys cause data loss?+
Yes. If your data source produces JSON with duplicate keys and your parser silently keeps only the last value, all previous values are discarded without any warning. This is a common source of hard-to-debug bugs when processing JSON from external systems or hand-edited files.
Does this tool fix duplicate keys automatically?+
Yes. After detecting duplicates, you can automatically fix them by keeping the first occurrence, keeping the last occurrence, or merging values into an array. The tool outputs the corrected JSON ready to copy.

Duplicate Keys in Practice — Code Examples

The following JSON contains a duplicate name key. While it may look intentional, the behavior when parsing it is entirely determined by the parser you use — not the JSON file itself.

JSON with duplicate key

{
  "user": {
    "name": "Alice",
    "age": 30,
    "name": "Bob"
  }
}

What parsers actually return for user.name

// JavaScript
JSON.parse(input).user.name   // → "Bob"  (last value wins)

# Python
json.loads(input)["user"]["name"]  // → "Bob"  (last value wins)

// Java (Jackson)
mapper.readTree(input).get("user").get("name").asText()  // → "Bob"

// Go encoding/json
var v map[string]interface{}
json.Unmarshal([]byte(input), &v)  // → "Bob"  (last value wins)

# Ruby JSON
JSON.parse(input)["user"]["name"]  // → "Bob"  (last value wins)

// PHP
json_decode($input, true)["user"]["name"]  // → "Bob"  (last value wins)

The silent overwrite means "Alice" is permanently lost with no warning or error. If the JSON was generated by a bug in your serializer, you would never know from the parsed output.

How Different JSON Parsers Handle Duplicate Keys

Language / Library Behavior with duplicates Error thrown?
JavaScript JSON.parseKeeps last valueNo
Python json.loadsKeeps last valueNo
Python with object_pairs_hookExposes all pairs — you decideNo
Java Jackson (default)Keeps last valueNo
Java Jackson (STRICT_DUPLICATE_DETECTION)Rejects the documentYes — JsonParseException
Go encoding/jsonKeeps last valueNo
PHP json_decodeKeeps last valueNo
Ruby JSON gemKeeps last valueNo
.NET System.Text.JsonKeeps last valueNo
jqKeeps last valueNo
Ajv (JSON Schema validator)Keeps last valueNo
JSON5Keeps last valueNo

The pattern is clear: virtually every mainstream parser silently keeps the last value with no warning. This means a duplicate key bug can survive undetected in production for months until a specific code path exposes the wrong value.

How to Detect Duplicate Keys with jq and Python

If you prefer the command line or need to automate duplicate detection in a CI/CD pipeline, here are two quick approaches:

Python — detect duplicates in a JSON file

import json

def find_duplicates(pairs):
    keys = [k for k, v in pairs]
    seen = set()
    for key in keys:
        if key in seen:
            print(f"Duplicate key found: {key}")
        seen.add(key)
    return dict(pairs)

with open("data.json") as f:
    json.load(f, object_pairs_hook=find_duplicates)

Node.js — detect duplicates programmatically

function parseWithDuplicateCheck(jsonString) {
  const seen = new Set();
  return JSON.parse(jsonString, (key, value) => {
    if (key !== '' && seen.has(key)) {
      console.warn(`Duplicate key detected: "${key}"`);
    }
    seen.add(key);
    return value;
  });
}

const data = parseWithDuplicateCheck('{"name":"Alice","name":"Bob"}');
// Console: Duplicate key detected: "name"

Explore more tools: All JSON Tools | Validator | Pretty Print | JSON Diff

Why Duplicate Keys Are Dangerous

The JSON spec (RFC 8259) says object keys "SHOULD be unique." It does not say they must be — so most parsers accept duplicate keys without throwing an error, but they handle them differently. The result is silent data loss that is extremely difficult to debug.

// Same JSON, different parsers:
const json = '{"name": "Alice", "name": "Bob"}';

// JavaScript JSON.parse: keeps LAST value
JSON.parse(json).name // "Bob"

// Python json.loads: keeps LAST value
// {"name": "Bob"}

// Java Jackson: keeps FIRST value
// {"name": "Alice"}

// Go encoding/json: keeps LAST value
// {"name": "Bob"}

// Result: silent data loss with no error!

How to Fix Duplicate Keys

Three approaches to resolve duplicate keys, ordered from least to most automated.

1. Manual Review

Use this tool to identify all duplicate keys and their values, then decide which value to keep based on business logic.

2. JavaScript Deduplication (keeps last value)

function deduplicateJSON(jsonStr) {
  // Re-serialising forces deduplication (keeps last value per JS behaviour)
  return JSON.stringify(JSON.parse(jsonStr));
}

3. Python Deduplication

import json
# json.loads already deduplicates (keeps last value)
deduped = json.loads(json_string)