Sort JSON Keys
Last updated: 26 September 2026
Put every object's keys in alphabetical order — recursively, so objects nested inside arrays are sorted too — while leaving array element order exactly as it was. The point is comparison: two payloads that differ only in key order start lining up line by line.
What sorting does to each shape
- Objects — keys are emitted in the order you choose. Keys are compared as plain strings, so
"B"sorts before"a"in ASCII order; mixed-case keys may not interleave the way a spreadsheet would sort them. - Arrays — never reordered. Element position is data in JSON, so
[3, 1, 2]stays[3, 1, 2]. Objects inside the array still get their keys sorted. - Nesting — the walk is depth-first through every object and array in the document, so a config file with objects inside arrays inside objects comes out fully sorted.
- Duplicates — if the input repeats a key, one of the values survives and the other is dropped when the document is parsed, before sorting begins. That is JSON's own behaviour, not this tool's.
Why sorted keys make diffs readable
Two API responses can describe the identical record yet produce a wall of red and green, because one backend emitted {"id": 7, "state": "paid"} and the other {"state": "paid", "id": 7}. The JSON grammar calls an object an unordered set of pairs, so both are equally correct — and every text diff is order-sensitive. Sort both sides first and the comparison collapses to the fields that actually changed. The same habit fixes three common situations:
- Snapshot and golden-file tests — a serialised expectation stops failing when a library changes the order it inserts keys.
- Config review — committed JSON that is always sorted produces readable history instead of churn.
- Merging two documents — matching structures sit next to each other, so manual merges stop being blind.
Sorting outside the browser
- jq —
jq 'walk(if type == "object" then to_entries | sort_by(.key) | from_entries else . end)'sorts every object recursively and leaves arrays alone. Needs jq 1.6 or newer forwalk. - Python —
json.dumps(data, sort_keys=True, indent=2, ensure_ascii=False). Thesort_keysflag already applies at every depth. - Node —
JSON.stringify(value, keys, 2)accepts an array of keys to order output, but that replacer only governs the top level, so a recursive rebuild is what you usually want. - Go —
map[string]Tmarshals with keys sorted already; ordered structs keep field order, so the language decides for you. - Command line against a file —
python3 -m json.tool --sort-keys config.jsonwrites the sorted document to stdout.
Where this differs from the formatter
The main tool does this same recursive sort as a toggle next to formatting, and adds line numbers, an indent width from 1 to 20, minification, download and character counts. Use it when sorting is one step in reading or cleaning a payload; use this page when sorting is all you want.
Frequently Asked Questions
Does sorting JSON keys change the data?
No. The JSON specification defines an object as an unordered set of name/value pairs, so reordering keys leaves the document semantically identical and parsing either form yields the same value. Arrays are the opposite case: their order is part of the data, so a sorter must reorder keys only and leave array elements exactly where they were.
Why would I want sorted keys?
To compare two documents. API responses and generated configs often carry the same fields in different orders, which makes a text diff look like everything changed. Sorting keys recursively first turns that noise into a diff that shows only real differences, and the same trick stabilises snapshot tests and reviewable config files.
How do I sort JSON keys in jq?
jq 'walk(if type == "object" then to_entries | sort_by(.key) | from_entries else . end)' rewrites every object in the document, including objects nested inside arrays, while leaving array order untouched. jq 1.6 or newer is required for walk.
How do I sort keys in Python or JavaScript?
Python does it in one call: json.dumps(data, sort_keys=True) sorts recursively through every object it finds. JavaScript has no built-in option, so either walk the structure and rebuild each object with sorted keys, or pass a sorted key array as the JSON.stringify replacer — noting that the replacer only affects the top level unless you build it recursively.
Does JSON have a canonical key order?
No. Because objects are unordered by definition, parsers are free to preserve insertion order — JavaScript does — or reorder them. That is precisely why sorting deliberately, with the same collation on both sides, is the reliable way to compare two files.