3 ms·
% echo '{"a": 5, "a": 42}' | jq { "a": 42 } Pretty much what I expected and also what you would get if you saw two instances of the same optional non
by jeffbee 6y ago
% echo '{"a": 5, "a": 42}' | jq
{
"a": 42
}
Pretty much what I expected and also what you would get if you saw two instances of the same optional non-repeated field in a protocol buffer message. What were you expecting or wanting?
- Groxx 6y agoTheir point was likely that implementations vary on the interpretation. Which is a bit of a problem for rpc systems.
- q3k 6y agoHere's the kicker: while this might sensible to you, some JSON implementations out there will reject duplicate fields (fail a parse completely), and the RFC does not even specify what is the correct behavior (override previous, ignore duplicates, fail entire parse, return non-unique keyed dictionary, something else entirely?). So while to you and I this behavior might be expected (although I'm still not sure that overriding previous fields is more obvious than ignoring repeated fields) - some library implements thought differently, and there isn't even an agreed on standard. Arguing about this isn't purely academic, either - there have been security vulnerabilities resulting from these differences [1]. [1] - https://justi.cz/security/2017/11/14/couchdb-rce-npm.html https://justi.cz/security/2017/11/14/couchdb-rce-npm.html
- jeffbee 6y agoInteresting. Protobuf specifies this last-instance-wins behavior, and it can be pretty useful. It allows you to override a field by simply appending a few bytes, without having to re-encode a whole message. JSON I guess doesn't have as much concern for efficiency as protobuf has.