← Back to the JSON tool

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.

Runs locally No upload Recursive Array order preserved

What sorting does to each shape

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:

Sorting outside the browser

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.