3 ms·
You make it sound like it's one of the laws of physics. ON part of JSON doesn't know about objects. When serialized, object entries are just an array of key-va
by plq 3y ago
You make it sound like it's one of the laws of physics.
ON part of JSON doesn't know about objects. When serialized, object entries are just an array of key-value pairs with a weird syntax and a well-defined order. That's true for any serialization format actually.
It's the JS part of JSON that imposes non-duplicate keys with undefined order constraint.
You are the engineer, you can decide how you use your tools depending on your use case. Unless eg. you need interop with the rest of the world, it's your JSON, (mis)treat it to your heart's content.
- mike_d 3y ago> You make it sound like it's one of the laws of physics. The text I quoted is from the RFC. json.org and ECMA-404 both agree. You are welcome to do whatever you want, but then it isn't JSON anymore.
- The_Colonel 3y agoThat does not follow. JSON formatted data remains JSON no matter if you use it incorrectly. This happens all the time in the real world - applications unknowingly rely on undefined (but generally true) behavior. If you e.g. need to integrate with a legacy application where you're not completely sure how it handles JSON, then it's likely better to use plain text JSON. You may take the risk as well, but then good luck explaining that those devs 10 years out of the company are responsible for the breakage happening after you've converted JSON to JSONB. In some cases, insisting on ignoring the key order is too expensive luxury, since it basically forces you to parse the whole document first and only then process it. In case you have huge documents, you have to stream-read and this often implies relying on a particular key order (which isn't a problem since those same huge documents will likely be stream-written with a particular order too).
- throwaway290 3y agoIt is very much still JSON, and your code can very much assume keys are ordered if your JSON tools respect it. ECMA-404 agrees: > The JSON syntax does not impose any restrictions on the strings used as names, does not require that name strings be unique, and does not assign any significance to the ordering of name/value pairs. These are all semantic considerations that may be defined by JSON processors or in specifications defining specific uses of JSON for data interchange. If you work in environments that respect JSON key order (like browser and I think also Python) then unordered behavior of JSONB would be the exception not the rule.
- sltkr 3y ago> It's the JS part of JSON that imposes non-duplicate keys with undefined order constraint. Actually since ES2015 the iteration order of object properties is fully defined: first integer keys, then string keys in insertion order, finally symbols in insertion order. (Of course, symbols cannot be represented in JSON.) And duplicate properties in JSON are guaranteed to be treated the same way they are in object literals: a duplicate overwrites the previous value but doesn't change the order of the key. Concretely that means if you write: Object.entries(JSON.parse('{"a":10, "1":20, "b":30, "a":40}')) This is guaranteed to evaluate to: [['1', 20], ['a', 40], ['b', 30]] (Note that '1' was moved to front, and 'a' comes before 'b' even though the associated value comes from the final entry in the JSON code.) Python made a similar change in version 3.6 (officially since 3.7), both with regards to insertion order and later values overwriting earlier ones while preserving order. I think the only difference at this point is that Python doesn't move integer-like keys to the front, because unlike JavaScript, Python properly distinguishes between different key types.
- iskela 3y agoAll the keys in JSON are strings. I do not understand why string literal containing number would be moved first. Maybe in JS-object in runtimes.