Search code examples
jsonrfc4627

Uniqueness of JSON object names as per RFC 4627


According to RFC 4627 section 2.2: 2.2. Objects

An object structure is represented as a pair of curly brackets surrounding zero or more name/value pairs (or members). A name is a string. A single colon comes after each name, separating the name from the value. A single comma separates a value from a following name. The names within an object SHOULD be unique.

But does "SHOULD be unique" conform to industry best practice? Do most JSON encoders/decoders enforce "MUST be unique". JSONlint.com enforces "MUST be unique".

For example { "foo":"value1", "foo":"value2" } returns Valid JSON, { "foo":"value2" }

And the second same name, replaces the first entry of the same name.


Solution

  • rfc 4627:

    The names within an object SHOULD be unique.

    Douglas Crockford (the author) said about it:

    This was the biggest blunder in the RFC. SHOULD should have been MUST.

    It is, sadly, too late to repair this.

    The recent ecma json standard doesn't require uniqueness to avoid invalidating existing json documents that are confirming to the rfc and that might contain duplicate names.

    In other words { "foo":"value1", "foo":"value2" } is a valid json but using duplicate names in new json documents is not recommended.

    Different parsers can behave differently (or can be configured to behave differently):