Hacker News new | past | comments | ask | show | jobs | submit login
Ask HN: Did JSON array order used to be a problem?
1 point by andrewfromx on May 5, 2023 | hide | past | favorite | 3 comments
Now-a-days everyone seems to rely on json that sends ["a","b","c"] over the wire will be parsed and stay in that order.

But years ago I remember developers telling me that certain languages json parsing languages implemented arrays differently so you could never rely on the order and had to do stuff like:

[{"a", "sort": 1},{"b", "sort": 2}, {"c", "sort": 3}]

because the client parsing that might end up with:

[{"c", "sort": 3},{"a", "sort": 1},{"b", "sort": 2}]

and then you'd have to re-sort it yourself. Was this a myth? Did old json parsers really have this problem?




Any JSON parser that re-orders array contents is broken. Just like any JSON parser that reverses the letters of your strings is broken. For objects, you generally shouldn't rely on them being in any particular order when you iterate them. I believe perl intentionally randomizes the order to discourage you from relying on it whereas python and ruby now promise they'll be in insertion order.


I don't recall that, just to not rely on the order of an object's keys, which appears to be outdated advice (at least for Javascript).

https://2ality.com/2015/10/property-traversal-order-es6.html


I recall the issue being with JSON objects getting treated as map values in the parsing language. You don’t always have a guarantee that iterating over the map will happen in the same order as was specified in the JSON. The guidance was usually to use JSON arrays instead, or have a sort order key as you put in there.




Join us for AI Startup School this June 16-17 in San Francisco!

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

Search: