Why sort keys?
JSON objects are unordered by definition, so sorting their keys never changes what a document means, but it makes documents far easier for people and tools to compare. Two API responses with the same data in a different key order produce a noisy diff; sorted, the diff shows only real changes. Sorted keys also make large configuration files easier to scan, keep version-control history clean when generated files are committed, and give a stable form for hashing or caching. For a side-by-side comparison of two documents, paste both into the JSON Diff tool, which can ignore key order on its own.
The options
- Order: A → Z (ascending) or Z → A (descending).
- Compare: how two keys are compared.
- Code point compares Unicode code points, the same rule as
sort_keysin Python’s json module andjq -S: all upper-case letters come before lower-case ones, soBsorts beforea, anditem10beforeitem2. - Case-insensitive ignores letter case and breaks ties by code point, so
aandBsort the way a dictionary would. - Natural also ignores case, but compares runs of digits as whole numbers, so
item2comes beforeitem10, andv1.9beforev1.10. Digit runs are compared exactly, however long they are.
- Code point compares Unicode code points, the same rule as
- Depth: All levels sorts every object in the document, including objects inside arrays. Top level only sorts just the outermost object’s keys, or for a top-level array, the keys of each object directly inside it, and leaves everything nested as it was.
- Indent: two or four spaces, tabs, or minified output on one line.
Arrays stay put unless you ask
Array order usually carries meaning: the steps of a pipeline, a ranking, a polygon’s coordinates, the history of an order. Reordering them can quietly change what the data says, so both array options are off by default and the page shows a warning while either is on.
- Sort arrays of values reorders arrays whose items are all strings, numbers, booleans or null: null first, then false and true, numbers in numeric order, then strings using the comparison you chose. Arrays that contain objects or other arrays are left alone.
- Sort arrays of objects by key reorders arrays of objects by the value of one key, such as
idorname. Objects without that key go to the end, and objects with equal values keep their original order.
Numbers are compared exactly, so 9007199254740993 sorts after 9007199254740992 even though JavaScript cannot tell them apart.
Nothing else changes
Sorting works on the parsed document without converting values, so everything other than order is preserved: 1.50 stays 1.50, 1E+400 is not turned into Infinity, and 20-digit IDs keep every digit. Duplicate keys are not merged; they stay in the output in their original relative order and are listed under the editors with their line numbers, because most parsers keep only the last one and you may want to fix them.
The sort is stable and therefore idempotent: sorting already-sorted JSON gives back the identical text. The work runs in a background worker in your browser, so multi-megabyte files stay responsive and nothing is uploaded. Input that is not valid JSON shows the parser’s error and location; JSON Repair can fix common damage first. The JSON formatter also has a simple sort-keys switch when you only need the default order while formatting.
Examples
A package manifest, sorted at every level
Top-level keys and the keys inside scripts and dependencies are all put in code point order.
{
"version": 2,
"name": "web",
"scripts": {"test": "vitest", "build": "astro build", "dev": "astro dev"},
"dependencies": {"preact": "^10.0.0", "astro": "^7.0.0"}
}{
"dependencies": {
"astro": "^7.0.0",
"preact": "^10.0.0"
},
"name": "web",
"scripts": {
"build": "astro build",
"dev": "astro dev",
"test": "vitest"
},
"version": 2
}Natural order for numbered keys
With Natural, item2 comes before item10, and Item1 and item1 sit together with the upper-case one first.
{"item10": "j", "item2": "b", "Item1": "a", "item1": "a2"}{"compare": "natural"}{
"Item1": "a",
"item1": "a2",
"item2": "b",
"item10": "j"
}Sorting an array of objects by name
With the key option set to name, the users array is reordered alphabetically by name; each object’s own keys are sorted too.
{"users": [{"name": "Linus", "id": 2}, {"name": "Ada", "id": 1}, {"name": "Grace", "id": 3}]}{"arrayKey": "name"}{
"users": [
{
"id": 1,
"name": "Ada"
},
{
"id": 3,
"name": "Grace"
},
{
"id": 2,
"name": "Linus"
}
]
}Common errors and how to fix them
| Error | Cause | Fix |
|---|---|---|
Duplicate key "id" — most parsers keep only the last valueExplained | The same key appears twice in one object. Both are kept in the sorted output, side by side. | Click the line number under the editors to jump to the duplicate and remove or rename one of them. |
JSON does not allow a comma after the last itemExplained | Sorting needs strictly valid JSON, and a trailing comma is not allowed. | Remove the comma at the marked spot, or open the text in JSON Repair. |
My array of objects did not change order | Arrays are sorted only when you turn an array option on, and arrays of objects only by a key you name. | Type the key to sort by, such as id, into Sort arrays of objects by key. |
Frequently asked questions
Does sorting keys change the meaning of my JSON?
No. The JSON standard defines objects as unordered, so any compliant parser reads sorted and unsorted documents as the same data. Arrays are ordered, which is why sorting them is opt-in.
Which comparison matches Python json.dumps(sort_keys=True) or jq -S?
Code point, the default. Both compare keys by Unicode code point, which puts upper-case letters before lower-case ones.
What does natural order do with numbers inside keys?
It compares each run of digits as a whole number, so file9 comes before file10, and it ignores case for the letters around them.
Can I sort only the top level?
Yes. Choose Top level only in Depth. For a document that is an array, that sorts the keys of each object directly inside the array.
Is it safe for confidential data?
Yes. Sorting happens in your browser, and the page never sends what you paste to a server.