Why canonical key order matters
JSON objects are unordered by definition, but the text you commit to a repository is not. If one service writes {"id":1,"name":"Ada"} and another writes {"name":"Ada","id":1}, the data is identical yet the two files differ on every line. Sorting keys into a single canonical order removes that noise: the same data always serializes to the same bytes, so a git diff highlights the fields that genuinely changed instead of a reshuffle.
Canonical order also makes comparing payloads practical. When you capture an API response before and after a change, sorting both sides means a line-by-line or text diff lines up field for field, and the one value that moved stands out immediately. Without sorting, a reordered key can make two equivalent responses look completely different.
The same property helps with caching and content hashing. A cache key or ETag derived from a JSON string is only stable if the serialization is stable. Sorting keys before you hash gives you a deterministic string, so logically equal objects produce the same key and you avoid duplicate cache entries for the same data. Snapshot tests benefit for the same reason — a sorted snapshot only churns when the data really changes.