Sort JSON keys alphabetically
Put every object’s keys into alphabetical order, at every level of nesting.
The reason to do this is comparison. Two API responses can be identical in meaning and completely different as text, purely because the keys came out in a different order. Sort both and a diff shows you only what actually changed.
Arrays are left alone. Their order carries meaning and reordering them would change the data rather than tidy it.
The result is the same data, only rearranged. Sorting keys changes the text and not the meaning, so anything that reads the JSON afterwards behaves exactly as it did before.
Sort keys alphabeticallyOnfixed by this page
Questions
- Does it reorder arrays too?
- No. An array's order is data, not formatting, so it is left exactly as it was. Only object keys are sorted.
- Why would I want this?
- Two JSON documents that hold the same data but list their keys differently look completely different in a diff. Sorting both makes the real differences visible.