[Issue 22612] std.json doesn't parse duplicate keys

d-bugmail at puremagic.com d-bugmail at puremagic.com
Mon Dec 20 15:03:57 UTC 2021


https://issues.dlang.org/show_bug.cgi?id=22612

--- Comment #3 from Răzvan Ștefănescu <rumbu at rumbu.ro> ---
(In reply to Salih Dincer from comment #1)
> Probably, but the 2nd object with the same name is overwritten with the 1st.
> In summary, data points to a single object.
> 
> NodeJS has the same effect:
> 
> const jsonStr ='{"name":"Salih","age":42,"name":"SALIH"}';
> const data = JSON.parse(jsonStr);
> 
> var assert = require('assert');
> assert(data.name != "Salih");

I don't expect anything else from JavaScript :)

I would never post this if I didn't encounter such json in the wild. Initially
I had the same reaction, but the client providing that json stream pointed me
to the ECMA standard and I lost all my arguments. I know that in the
corresponding RFC says that you SHOULD not have duplicate keys, but SHOULD is
interpreted by some people as "CAN".

There are npm packages even for node.js which can handle duplicate keys. The
ideea is that as long as duplicate keys are allowed in the standard, at least
std.json can provide in the documentation that last value wins (there are other
approaches - first value wins, error, silently transform duplicate keys in
arrays).

--


More information about the Digitalmars-d-bugs mailing list