Checking duplicate property names before relying on formatted JSON
A JSON formatter can accept a document with a repeated property name and print both copies unchanged. This worksheet shows what that result does and does not tell you.
Valid syntax is a low bar
A formatter checks that a document follows JSON grammar: braces match, strings are quoted, commas sit in the right places. Meeting that grammar is what valid means for the tool. A document with two properties named color passes, because the grammar allows a name to appear more than once. The JSON validator formatter parses the document, finds no syntax error, and prints it at the requested indentation with both occurrences intact. The green result therefore answers a narrow question, whether the text is well formed. It does not answer whether the data are consistent, complete or what the consuming application will make of them. Validity says nothing about whether names are spelled consistently or whether a required field is present. A spell checker confirms every word exists without telling you whether the sentence is true, and the formatter plays the same role: it confirms the grammar and leaves the meaning to people. Keep that in mind whenever the tool reports valid for a document you did not write yourself.
What the specification says
RFC 8259, the current JSON standard, says that the names within an object should be unique. The word should marks a recommendation and not a requirement. The same document notes that when names are not unique, the behavior of software receiving the object is unpredictable. Many implementations accept the document and keep the last value, some keep the first, and some report an error. A stricter profile named I-JSON, described in RFC 7493, requires that objects do not contain duplicate names. Documents that follow I-JSON are safer to exchange, since every parser agrees about what they say. The standard leaves room for both behaviors, so a document with duplicates is allowed to exist and is not guaranteed to be portable.
The worksheet example
The example is the compact document with the name color appearing twice, once with the value red and once with blue. Formatting with an indentation of zero returns the same two members in the same order, and the validity flag is true. A consumer that keeps the last value will see blue. One that keeps the first will see red. One that rejects duplicates will refuse the document entirely. Nothing in the formatted output hints at this disagreement, which is why the output is not an audit of the data. Most developers discover this only when a configuration change appears to have no effect, because a later or earlier copy of the name overrides the one they edited.
How common parsers behave
In JavaScript, JSON.parse keeps the last value for a repeated name, and the earlier value is silently discarded. Python's standard json module behaves the same way by default, though it provides a hook that lets a program see all pairs. Other libraries in other languages vary, and some strict parsers raise an error. Because behavior differs, a document with duplicates can mean different things on different systems. If two components in a pipeline use different parsers, one may validate the document while another acts on a different value than the first one saw. Streaming parsers that report each pair as it is read can expose duplicates, whereas parsers that build an object in memory overwrite them as they go.
Why it matters beyond style
The disagreement has practical consequences. A configuration file with a repeated key may change behavior depending on which tool reads it, which is confusing to debug. In security-sensitive settings, differences in how parsers treat duplicates have been used to slip data past a validator that sees one value while the application acts on another. Treating duplicate names as an error at the point of entry removes that class of problem. The formatter in this worksheet does not take that position, so a check for duplicates has to be a separate step. Logging the raw document before parsing helps when you need to prove which version of a name was actually received.
Deciding what the duplicate means
Before changing a document, work out why the name is repeated. It may be a copy-and-paste error, in which case one occurrence should go and the person responsible should say which. It may be a merge of two sources where a later value is meant to override an earlier one, in which case the override rule should be explicit and applied before serialization. It may be an attempt to represent several values, in which case the correct JSON is a single name with an array, such as color holding red and blue as list items. Each fix changes the data, and none is a formatting decision. If nobody can say which value is right, the source should be corrected before any formatting is applied. A short note in the commit message or the ticket that records which value was chosen, and why, saves the next reader from repeating the investigation.
Using the unique-name control
The control document replaces the repeated name with two distinct names, first and second, holding the same two values. Formatting returns it unchanged and valid. This shows what a clean version might look like when the two colors are separate fields. It is a different structure, though, and the tool made no decision to restructure the original. Changing color to first and second altered the names that other software may look for, so the consumer of the data has to agree with the change. The example illustrates a structure and does not repair the first document. The control also demonstrates why the original document was risky. When two names are distinct, every parser agrees on both values, and nothing depends on a tie-break rule.
Duplicates in nested objects
Duplicate names can occur at any depth. A document may be fine at the top level and still repeat a name inside one of its nested objects, where it is much easier to miss. For example, an address object might list city twice after a careless merge. The formatter will print every nested occurrence it finds. When reviewing a large file, a search for a specific name and a count of occurrences within one object are quicker than reading the whole thing. A schema validator or a parser option that reports duplicates can do the same job automatically. Large exported files can hide a duplicate in the fortieth record out of thousands, so a script that counts how many times each name appears within each object finds repeats far faster than reading, and it can run on every new export.
Indentation and the displayed result
The worksheet uses an indentation of zero, which produces a single line with no extra spaces. With an indentation of 2, the same document would print one member per line, and the two color lines would appear one above the other, which makes the repetition easier to see. The data are the same either way. Formatting changes only whitespace, so it is a poor place to expect corrections. If the duplicate is visible in the pretty-printed output, treat that as a prompt to investigate, and not as something the tool has handled. Use a nonzero indentation when you want to read a document and zero when you want to compare two documents byte for byte. Neither setting repairs anything, and a clean looking layout can give a false sense that the document has been reviewed.
Building a safer workflow
A cautious pipeline checks for duplicates at the boundary, using a parser or linter configured to reject them. It keeps the original document alongside any reformatted copy, so changes can be reviewed. It documents which value wins when a merge is intended. Finally, it avoids relying on a parser-specific tie-break, since that couples the data to one library. These steps cost little. They turn an ambiguity that the grammar permits into an error that people can see and fix. Automated tests that feed a duplicate-name document to each parser in the pipeline reveal disagreements early. If you control the producer of the document, fix the problem there, since correcting it downstream only hides the cause.
Other syntax the formatter checks
Besides duplicate names, the grammar rules out several habits that are common in hand-written configuration files. JSON has no comments, so lines beginning with slashes or hash signs are errors. A trailing comma after the last member is not allowed. Names must be in double quotes, and single quotes are not accepted for strings. Numbers cannot have leading zeros or a leading plus sign. A document that breaks these rules fails the formatter with a syntax message, whereas duplicate names pass. The contrast is instructive. The grammar rejects some risky habits outright and quietly permits others, so a clean result from the tool leaves the duplicate-name question open. String escapes are checked as well, so a stray backslash that does not form a valid escape sequence is a syntax error.
A merge that creates duplicates
Duplicates often appear when two objects are joined by text concatenation instead of by a data-aware merge. Imagine a defaults object that sets color to red and a user object that sets color to blue. Gluing their members into one object produces the worksheet document. A proper merge step would decide that the user value overrides the default and write the name once. The corrected result contains a single color member with the value blue, and no consumer has to guess. When a process builds JSON from fragments, merge at the data level, then serialize once, and the duplicates cannot arise.
Reproduce the checks
Repeated property name
Open the Json Validator Formatter and enter:
- JSON input
{"color":"red","color":"blue"}- Indent
0
Expected output:
Valid
trueFormatted
{"color":"red","color":"blue"}Both source tokens remain in the formatted representation.
Distinct property names
Open the Json Validator Formatter and enter:
- JSON input
{"first":"red","second":"blue"}- Indent
0
Expected output:
Valid
trueFormatted
{"first":"red","second":"blue"}This is another data structure, not an automatic repair.
References and implementation scope
The examples describe the behavior of the linked Computandum tools. A successful local check does not establish that another application accepts the same format or applies the same limits.
Sources
- Primary definition and convention reference ↗ · 2026-10-08