🔤 JSON Key Sorter
By ToolNimba Web Dev Team · Updated 2026-06-19
Paste JSON above, then click Sort Keys to order every object key alphabetically.
This JSON Key Sorter takes any valid JSON and reorders every object key alphabetically, all the way down through nested objects. Array element order stays exactly as it was, because arrays are ordered by position, not by name. Choose ascending (A to Z) or descending (Z to A), pick your indentation, and copy the clean, sorted result. Everything runs in your browser, so your data never leaves the page.
What is the JSON Key Sorter?
JSON objects are, by the specification, an unordered collection of name and value pairs. That means {"b":1,"a":2} and {"a":2,"b":1} represent the same data. In practice, though, the textual order of keys matters a great deal for humans and tools: it determines how a file reads in a code review, whether two config files produce an identical diff, and whether a checksum or snapshot test matches. Sorting keys into a single canonical order removes that noise so the only differences you see are real value changes.
Sorting keys is a recursive job. The tool walks the parsed structure: when it meets an object it sorts that object's own keys, then descends into each value and sorts any objects it finds nested inside. When it meets an array it leaves the element order untouched (reordering array items would change the meaning of the data) but still recurses into each element so that objects sitting inside the array get their keys sorted too. Primitive values (strings, numbers, booleans and null) are passed through unchanged.
The comparison itself is a plain lexicographic (dictionary) sort on the key strings, using JavaScript's default string ordering by Unicode code point. That puts uppercase letters before lowercase ones (so "Zoo" sorts before "apple"), and it sorts digits before letters. If that surprises you, switch on the case-insensitive option, which folds keys to lowercase before comparing so "Apple" and "apple" land next to each other. The output is then re-serialized with your chosen indentation, giving you a tidy, deterministic, copy-ready document.
When to use it
- Producing a canonical, diff-friendly version of a config or data file so version control shows only real changes.
- Tidying an API response or fixture so related keys are easy to scan and locate by eye.
- Normalizing two JSON files before comparing them, so key ordering does not create false differences.
- Preparing JSON for snapshot tests or checksums where a stable, repeatable key order is required.
How to use the JSON Key Sorter
- Paste or type your JSON into the input box.
- Choose the sort order: A to Z (ascending) or Z to A (descending).
- Pick the indentation (2 spaces, 4 spaces or tab) and, if you like, tick case-insensitive.
- Click Sort Keys to see the recursively sorted JSON, then click Copy to grab the result.
Formula & method
Worked examples
You have the object {"name":"Ada","age":36,"city":"London"} and want keys A to Z.
- Parse the JSON into an object with keys name, age and city.
- Collect the keys: ["name","age","city"].
- Sort them lexicographically ascending: age, city, name.
- Rebuild the object in that order and pretty-print it.
Result: { "age": 36, "city": "London", "name": "Ada" }
You have a nested object {"b":{"z":1,"a":2},"a":[3,2,1]} and want keys A to Z.
- Top-level keys b and a are sorted to a, b.
- The value of a is the array [3,2,1]: array order is preserved, so it stays [3,2,1].
- The value of b is an object {"z":1,"a":2}: its keys are sorted to a, z.
- The result reflects sorting at every object level while the array keeps its order.
Result: { "a": [ 3, 2, 1 ], "b": { "a": 2, "z": 1 } }
What the sorter does to each JSON value type
| Value type | Action taken |
|---|---|
| Object | Keys sorted alphabetically; values recursed into |
| Array | Element order preserved; each element recursed into |
| String / number / boolean | Returned unchanged |
| null | Returned unchanged |
How ordering options affect the keys Apple, banana and Cherry
| Option | Resulting key order |
|---|---|
| Ascending, case-sensitive | Apple, Cherry, banana |
| Ascending, case-insensitive | Apple, banana, Cherry |
| Descending, case-sensitive | banana, Cherry, Apple |
| Descending, case-insensitive | Cherry, banana, Apple |
Common mistakes to avoid
- Expecting array items to be sorted. This tool sorts object keys, not array values. Array order carries meaning (it is ordered by position), so reordering items would change your data. Elements inside an array still have their own keys sorted, but the items themselves stay where they are.
- Forgetting that uppercase sorts before lowercase by default. The default comparison is by Unicode code point, so a key like "Zebra" comes before "apple". If you want "Apple" and "apple" grouped together, switch on the case-insensitive option.
- Assuming sorting changes the meaning of the JSON. JSON objects are defined as unordered, so reordering keys produces an equivalent document. The values, types and structure are identical; only the textual order of keys changes.
- Pasting JavaScript objects instead of strict JSON. Unquoted keys, single quotes, trailing commas and comments are valid JavaScript but not valid JSON, so the parser will reject them. Wrap keys and strings in double quotes and remove trailing commas first.
Glossary
- Key
- The name part of a name and value pair inside a JSON object, always a string.
- Lexicographic order
- Dictionary-style ordering of strings, comparing characters one position at a time.
- Recursive sort
- Sorting that descends into nested objects and into objects held inside arrays, not just the top level.
- Canonical form
- A single, agreed representation of data (here, keys in a fixed order) so equivalent values look identical.
- Pretty-print
- Serializing JSON with line breaks and indentation so it is easy for a human to read.
Frequently asked questions
Does sorting keys change my data?
No. JSON objects are defined as an unordered set of name and value pairs, so reordering the keys produces a document that means exactly the same thing. All values, types and nesting stay identical; only the textual order of the keys changes.
Are arrays reordered too?
No, array element order is preserved. Arrays are ordered by position, so changing their order would change the meaning of your data. The tool does recurse into each array element, so any objects inside an array still get their own keys sorted.
Does it sort nested objects or only the top level?
It sorts every object at every depth. The sorter walks the whole structure, ordering the keys of nested objects and of objects that sit inside arrays, so the entire document ends up in a consistent order.
Why do uppercase keys sort before lowercase ones?
The default comparison uses Unicode code point order, where uppercase letters come before lowercase. That places "Name" before "age". Turn on the case-insensitive option if you want keys grouped without regard to letter case.
What happens if my JSON is invalid?
The tool shows a clear error message with the reason from the JSON parser and leaves the output empty. Common causes are single quotes, unquoted keys, trailing commas or comments, none of which are valid JSON.
Is my JSON sent anywhere?
No. All parsing and sorting happen in your browser with plain JavaScript. Nothing is uploaded, stored or sent over the network, so it is safe to use with private or sensitive data.