Conversation

given this JSON object, which of these are correct parses according to ECMA 404, json.org, and RFC 8259?

{
  "a": 123,
  "a": 456
}
3
3
0

only fake JSON fans will get this one wrong 😤

0
0
0
a
Show content

@charlotte well. none of those documents specify what happens with multiple names. that gets delegated to "the land of implementation". so it could be any of those

1
0
0
re: a
Show content

@charlotte you could make an argument that this is analagous to "implementation-defined behavior" in C, and as such the set of correct parses according to the document is the union of how every possible individual implementation consistently parses it itself
or you could just as easily argue it's "undefined behavior" and thus anything goes

1
0
0
re: a
Show content

@Yoshi defining syntax without semantics makes json IMO useless since every single implementation can implement it however it pleases, which isn’t even just a theoretical concern because maliciously formatted json documents do just parse differently between different json parsers and since every json parser i have used accepts such constructions silently there really isn’t a good way to find it out

anyways regardless nobody has gotten all the answers right yet

2
0
0
re: a
Show content

@charlotte @Yoshi I maintain "error" because this is something that can only occur on JSON parse. After that, it'd be redefining.

I know that isn't what consistently happens but I've definitely seen ESLint throw a hard Error instead of Warning about the same key twice in the same object level, many times.

1
0
0
re: a
Show content

@KayOhtie @Yoshi error is one valid answer yes.

but all the other answers are equally valid according to the spec. javascript JSON.Parse will do the 4th one

1
0
1
re: a
Show content

@KayOhtie @Yoshi the thing is that json doesn’t specify any sort of semantics for it. the rfc handwaves these concerns away too while also saying possible parser actions or what is Common in parsers

1
0
1
re: a
Show content

@KayOhtie @Yoshi but anyways one thing that literally has happened and has caused security vulnerabilities before is something like this:

{
  "auth": {"scope": "admin"},
  "auth": {"scope": "user", "sig": "lvfgxhcqfvglchgqflcfg"}
}

passed to the authentication service it sees the second auth and grants access, passed to the admin service it sees the first one and grants admin access

0
0
1
re: a
Show content

@charlotte I wrote up my reply as me saying "all of these could be valid parses because it's left to the implementation in any case" but I spent too long thinking about my words that I forgot to actually put my answer in the poll

0
0
0

Simon Tesla 🏳️‍🌈🐭

@charlotte Voted 456 (which is what happens in FF and presumably other browsers) and Error (which is what should happen IMHO) and surprised to see that the spec says it's semantics outside of parse scope. Feel like you'd want a common spec for semantics in there as well, but I guess people assume it'd typically inherit JS object literal semantics which I do believe is specified for ECMAscript.

0
0
0