Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

According to the json spec

    "An object is an unordered set of name/value pairs."
So you should not be relying on ordering of json fields.


In theory, correct. In practice, here are the two use cases which that approach breaks:

1. Cache control using etags. If the content changes by a single byte, even if semantically identical, the etag should change. Hence '{"foo":1,"bar":2}' is not equivalent to '{"bar":2,"foo":1}'. I can see serving such information directly, or embedding it into a larger JSON response.

2. Committed JSON files. This is an anti-pattern, but one I've seen many times. In one case, it was a translation file that was generated in a separate project. When I joined, it had been like that for more than 5 years. It was like that when I left, although I at least monkey patched REXML to emit sorted attributes in XML (also unordered in theory).

So while in practice, I don't depend on a specific ordering, but I need the order (whatever it is) to be stable. For all I care, it could be sorted by the cryptographic hash of the keys, so long as it is consistent.

Just to gather what others have said here: JSONB appears to store fields sorted lexicographically for binary search purposes, and emits them in that order as well. This could be undocumented behavior, yet important for the above two use cases.


You can still use the 'json' type (not 'jsonb'), which preserves the text representation.

Or you can sort the keys on serialization when you serve or store the JSON.


For the former, you really need to sort the keys. If you ever end up passing the JSON through any other encode/decode step, your ETags will break and it won't be obvious why.

For hand-written JSON, I'm not sure there's a good solution besides modding the users' editors to sort for them.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: