Quick answer

JSON allows duplicate keys in an object, but parsers typically keep only the last value.

JSON Duplicate Keys

JSON allows duplicate keys in an object, but parsers typically keep only the last value. Duplicate keys often indicate a mistake.

Common causes

How to fix

JSON duplicate keys can create subtle data bugs because the JSON specification allows repeated names in an object, but many parsers resolve them by keeping only the last value. That means a payload may look valid at a glance while still producing unexpected results in applications, APIs, configuration files, or data pipelines. This page helps developers, QA teams, and API consumers understand how duplicate keys behave, why they happen, and how to fix them before they cause inconsistent reads or overwritten values.

How This Validator Works

This checker inspects a JSON object for repeated property names within the same object scope. When duplicate keys are present, it flags the repeated names so you can review whether the issue is intentional or accidental. In many environments, the final parsed value is the last occurrence, but behavior can vary by parser, language runtime, or downstream tooling. The goal is to surface ambiguity early so you can normalize the data before it reaches production systems.

Common Validation Errors

Where This Validator Is Commonly Used

Why Validation Matters

Duplicate keys can make structured data harder to trust because the visible JSON text may not match the parsed result. In API systems, that can lead to overwritten fields, inconsistent business logic, or hard-to-reproduce bugs. In configuration contexts, duplicate names can hide the effective setting and complicate troubleshooting. Validating for duplicates improves data clarity, reduces parser ambiguity, and helps teams maintain predictable behavior across tools and runtimes.

Technical Details

In JSON objects, member names are expected to be strings, and the specification does not require unique enforcement in the text itself. However, many parsers and libraries treat duplicate names as a last-wins scenario, while some may preserve the first value, keep all values, or expose warnings. Because behavior is implementation-specific, duplicate keys should be treated as a data quality issue even when the input parses successfully.

Scope Duplicate detection applies within the same object, not across separate nested objects.
Typical parser behavior Many parsers keep the last occurrence of a repeated key.
Risk Silent overwrites, inconsistent application behavior, and debugging difficulty.
Best practice Ensure each object has unique keys before serialization or release.

FAQ

Are duplicate keys allowed in JSON?

JSON text can contain duplicate keys in an object, but that does not mean the result is safe or predictable. Many parsers accept the input and then resolve the conflict by keeping only one value, often the last one. Because behavior can vary by implementation, duplicate keys should be treated as a validation problem rather than a normal pattern.

What happens when a JSON object has the same key twice?

In many common parsers, the later value overwrites the earlier one. Some tools may preserve the first value, warn about the issue, or handle it differently depending on the language or library. This is why duplicate keys can be dangerous in APIs and configuration files: the parsed result may not match what the author intended.

Why do duplicate keys happen?

They often appear when objects are merged, when templates generate overlapping fields, or when code serializes the same property more than once. Manual editing can also introduce duplicates. In larger systems, the issue may come from multiple layers of transformation where one field is added after another without checking for collisions.

How do I fix duplicate keys in JSON?

Remove the repeated property so each key appears only once in the object. If the values are both needed, consider restructuring the data into an array, nested object, or separate fields with distinct names. After editing, revalidate the JSON and confirm that the parsed output matches the intended structure.

Can duplicate keys break APIs?

Yes, they can cause inconsistent request handling or unexpected response interpretation. If an API consumer sends duplicate keys, the server may read a different value than the sender expected. If an API returns duplicate keys, downstream clients may parse the payload differently. That can lead to subtle bugs that are hard to trace.

Do all JSON parsers handle duplicates the same way?

No. Parser behavior depends on the language, runtime, and library. Some keep the last value, some keep the first, and some may expose all occurrences or emit warnings. Because of that variation, relying on duplicate keys is not portable and can produce different results across systems.

Is a JSON object with duplicate keys invalid?

It may still parse successfully in many tools, but it is still considered a data quality issue. The problem is not always syntax failure; it is ambiguity. Even when the parser accepts the input, the meaning of the object can be unclear or implementation-dependent, which is why validation is important.

How can I prevent duplicate keys in generated JSON?

Use a single source of truth for each field, validate object construction before serialization, and add tests that check for repeated names. If you merge objects programmatically, make sure collisions are handled intentionally. Linting, schema checks, and duplicate-key validation can help catch issues before release.

Should I use arrays instead of repeated keys?

If you need multiple values for the same concept, an array is usually the clearer choice. Arrays preserve order and make the structure explicit, while duplicate keys can be overwritten or ignored depending on the parser. Using an array improves portability and makes the data easier to validate and consume.

Related Validators & Checkers

FAQ

What happens with duplicate keys in JSON?
Most parsers use the last occurrence; earlier values are overwritten. Behavior can vary.
Is duplicate key valid JSON?
Yes, it is valid per the spec, but it is usually a logic error.

Fix it now

Try in validator (prefill this example)

Related

All tools · Canonical