JSON Key Sorter (A-Z / Z-A)
Reorder keys at every nesting level so two payloads that differ only in field order become directly comparable, instead of producing a diff where every line changed.
The same data serialized by different libraries, different languages or different client versions often comes out with its keys in a different order. A text comparison of two such documents lights up every line even though the data is identical, and the one field that actually changed is buried in the noise. Sorting both sides by the same rule collapses that noise down to the real differences — and this tool sorts recursively, not just the top level.
Sorting requires parsing, so the input has to be valid JSON. A syntax error is reported directly rather than producing a partially sorted result. Processing is local to the browser, so both documents in a comparison stay on the machine, which is what makes it usable for order records, user profiles or anything else that should not be sent to a third party for a look.
Sorting rules and what they apply to
Sorting is recursive: the members inside each object are reordered by key name, while arrays themselves are never reordered, because the sequence of an array is part of the data and alphabetizing a list would change its meaning. Objects nested inside array elements are still sorted, which is what you want in a list of records with varying field order. Comparison uses locale-aware collation rather than raw byte comparison, so uppercase and lowercase letters do not fall in ASCII code-point order, and keys beginning with a digit generally precede letters. Because collation interleaves cases roughly the way a dictionary does, B does not automatically sort before a as it would in byte order.
Why this removes false differences
The members of a JSON object are unordered by definition: the specification guarantees which keys exist, not the sequence they appear in, so {"a":1,"b":2} and {"b":2,"a":1} are semantically equal documents. A line-based diff, however, compares positions, so a change in key order marks every affected line as modified. Sorting both sides first reduces the comparison to the fields whose values actually changed. The diff tool on this site embeds the same operation as a "sort keys then compare" button applied to both panes, so normalizing here and normalizing there produce identical results.
Output format and typical uses
Sorted output is always emitted with two-space indentation, which means sorting also formats the document; the original whitespace is not preserved. There are three common uses: comparing two API responses, normalizing configuration files into one consistent style before they are committed, and generating stable snapshots for automated tests so assertions do not fail merely because key order shifted between runs. Two warnings. Sorting permanently changes the text, so copy the original if presentation order mattered. And duplicate keys were already resolved during parsing, with the later value winning, so sorting cannot bring back what the parser discarded.
Frequently asked questions
- Does sorting disturb the elements of an array?
- No. Array order is treated as data. The tool recurses into objects contained in array elements and sorts their keys, but the sequence of the elements themselves is untouched. Product listings, log entries and any other ordered collection can be sorted without the list being rearranged.
- Why is the result not in the case order I expected?
- Because comparison follows locale collation, not ASCII code points. Under byte ordering every uppercase letter precedes every lowercase letter, so B sorts before a. Under dictionary-style collation the cases interleave, which is closer to what a person expects. If you need strict code-point ordering for a machine comparison, adjust the handful of affected keys by hand after sorting.
- How are non-ASCII keys sorted?
- By the browser locale collation rules for those characters. Results can vary with the system language and the browser implementation, so two machines may disagree on the final order. If the sorted output is used as a snapshot that automated tests assert against, keep key names in ASCII so the result is deterministic.
- How is sorting different from formatting?
- Formatting changes whitespace and indentation while leaving key order alone. Sorting changes key order and, as a side effect, re-emits the document with two-space indentation — so a sorted document is always formatted, but a formatted document is not necessarily sorted. Use formatting to align indentation and sorting to eliminate key-order differences.