jsonlint icon indicating copy to clipboard operation
jsonlint copied to clipboard

Duplicate keys aren't cool!

Open thejh opened this issue 14 years ago • 27 comments

$ echo '{"a":1,"a":2}' | jsonlint
The "sys" module is now called "util". It should have a similar interface.
{
  "a": 2
}
$ 

thejh avatar Nov 12 '11 19:11 thejh

By default we simply follow the spec, but it wouldn't hurt to have as an option.

zaach avatar Nov 13 '11 00:11 zaach

I guess I expect something with "lint" in the name to do some plausibility checking.

thejh avatar Nov 13 '11 01:11 thejh

+1 Any chance this will get fixed? Also, I'd think it should complain about empty-string keys:

{ "": "foo" }

Klortho avatar Jan 02 '13 02:01 Klortho

+1 just got bitten by a bug caused by a duplicate key, would love to use a tool such as jsonlint to catch these errors in my automated tests.

rngadam avatar Sep 18 '13 17:09 rngadam

JavaScript, JSON, and Python all allow a key to be duplicated. (It is NOT a syntax error; the value of the last occurrence of the key is kept.)

JavaScript, JSON, and Python all allow the empty string as a valid key. (It is NOT a syntax error.)

So my vote is that these are NOT flagged as error or even warnings, unless the JSONLint user specifically calls for them via a commandline option.

dharasty avatar Jan 24 '14 22:01 dharasty

@dharasty : what's the point of a lint tool if it doesn't catch anything more than the real parser? I think it's more than likely that there's an issue when there are duplicate keys...

rngadam avatar Jan 28 '14 15:01 rngadam

@rngadam checking for duplicate keys was a big problem for me, and since this issue is still unresolved after 2 years, I had to bite the bullet and implement my own checker.

I agree with your position -- most people expect keys to be unique, key/value pairs to not be silently discarded, and the resultant object to match the JSON input. All of these reasonable expectations are broken, making debugging quite painful. :+1:

mattfenwick avatar Jan 28 '14 16:01 mattfenwick

I see your points (@rngadam and @mattfenwick) and understand that if this as "bitten" you, you'd like a checker.

I guess I just don't use linting tools that way: I tend to use them to validate syntax and to pretty-format.

I do scratch my head a bit at "most people expect ... the resultant object to match the JSON input". I'm not sure what that means if the JSON input in the presence of multiple identical keys, which is allowed by in the serialization defined in the JSON spec, but clearly not in the concept of a JSON (or JavaScript) object. Therefore I'm not sure what is "reasonable" to expect.

I -- for one -- expect that repeated keys silently trump prior instances of the same key... but just because it it my experience that that is true (in JSON, JavaScript, and Python).

Is this your view?: "because purposeful use duplicated keys are very rare, when seen, it JSONLint should emit a warning".

dharasty avatar Jan 28 '14 17:01 dharasty

+1

Delagen avatar Dec 19 '14 11:12 Delagen

+1

odino avatar Apr 21 '15 07:04 odino

I would also like to have jsonlint emit a warning on duplicate keys.

pixelastic avatar May 19 '15 12:05 pixelastic

+1

itsikavidan avatar Jun 17 '15 12:06 itsikavidan

It actually happened to me at least once that a duplicate key written manually by mistake caused a real bug, so it would be nice to get a warning about that. In my case I'd prefer that to be a fatal error in the build that would force me to fix it before deployment.

amire80 avatar Jul 08 '15 09:07 amire80

:+1:

hotoo avatar Aug 24 '15 03:08 hotoo

100% agree. There is no reason to purposefully hard code duplicate keys in a JSON file. A linter should definitely check for this. Via an option is reasonable, considering it's not part of the JSON spec. Any chance this will get added? Need help creating a PR?

mikeabiezzi avatar Dec 30 '15 23:12 mikeabiezzi

A pure JavaScript version of the service provided at jsonlint.com.

Judging from readme, this projects claims to work like jsonlint.com, but compared to jsonlint.com - it doesn't detect duplicates.

azamat-sharapov avatar Sep 16 '16 18:09 azamat-sharapov

We use this library for linting manually written json files, and this problem caused confusion and time wasted more than once. I'd be happy to have it as an option if it shouldn't be the default behavior...

adbl avatar Nov 10 '16 10:11 adbl

any chance to get this feature?

hypery2k avatar Jan 12 '17 18:01 hypery2k

Finally, is there a check that supports duplicate keys?

Christian-Yang avatar Apr 17 '17 02:04 Christian-Yang

Would love this.

AndersDJohnson avatar May 15 '17 20:05 AndersDJohnson

Would love to see this also.

WindRider7 avatar Jun 07 '17 15:06 WindRider7

7 years later, still nothing?

kkor avatar Jul 31 '18 14:07 kkor

9 years later, humanity is on the verge of extinction (COVID-19) - still nothing...(

GGurbanov avatar Apr 06 '20 14:04 GGurbanov

OMG, this issue is still open.

FuDesign2008 avatar Aug 26 '20 09:08 FuDesign2008

I stumbled on this thread, for this exact problem.

The jsonlint-mod fork seems to solve that problem and exposes the same bin.

Posting here in case it helps someone else.

jpalardy avatar Sep 16 '20 20:09 jpalardy

I know duplicate keys are valid, but I'd imagine most occurrences are a mistake and unexpected and unwanted.

rdmarsh avatar Feb 09 '22 01:02 rdmarsh

https://github.com/prantlf/jsonlint seems to be maintained

HolgerJeromin avatar May 14 '24 12:05 HolgerJeromin