Overview
Sort JSON object keys alphabetically
JSON objects are unordered by definition, but the order keys appear in the text matters a lot in practice. Sorted keys make a file easier to scan, easier to compare, and far easier to diff in version control. Two files that hold the same data but list keys in a different order will produce noisy diffs, even though nothing actually changed.
ToolHub JSON Key Sorter reorders every object key alphabetically, recursively, including objects nested inside arrays. Array element order is always preserved, because the position of items in an array is meaningful. The result is pretty-printed with 2-space indentation. Everything happens in your browser.
Step-by-step
How to sort JSON keys
- 1
Paste your JSON
Drop any valid JSON object or array into the input panel. The output updates live as you type or paste. - 2
Choose a direction
Use the Ascending or Descending toggle to sort keys from A to Z or from Z to A. - 3
Copy the result
Click copy to grab the sorted, pretty-printed JSON and paste it back into your file or config.
Background
How key sorting works
The tool parses your input with JSON.parse, then walks the entire structure. For every object it encounters, it rebuilds the object with its keys sorted. For every array, it keeps the elements in their original order but still sorts the keys of any objects inside those elements. Numbers, strings, booleans, and null are left exactly as they are.
Why array order is preserved
In JSON, an object is a set of key-value pairs with no guaranteed order, so sorting keys does not change meaning. An array is an ordered list, where position carries information. Reordering array elements would change the data, so the tool never touches array order. Only the keys of objects get sorted.
Ascending versus descending
Ascending sorts keys from A to Z, which is the most common choice and matches the output of many formatters and linters. Descending sorts from Z to A, which can be useful when you want the most recently named or highest-priority fields to appear first.
Use cases
When to sort JSON keys
Cleaner git diffs
Normalize key order before committing config or fixture files so diffs show only real changes.
Comparing two payloads
Sort both API responses the same way to spot genuine differences without key-order noise.
Stable config files
Keep package manifests, settings, and translation files in a predictable, alphabetical order.
Snapshot testing
Produce deterministic JSON so test snapshots stay stable across runs and machines.
Readable documentation
Sorted keys make sample payloads in API docs easier to read and reference.
Deduplicating data
Canonical key order helps you detect objects that hold identical data in a different layout.
Tips and best practices
- Sorting keys never changes values, only the order keys appear in the text.
- Array element order is always kept intact, since position is meaningful in arrays.
- Sorting is case-sensitive by locale rules, so uppercase and lowercase keys may interleave based on your environment.
- Use sorted output as a canonical form before comparing two JSON files.
- If parsing fails, check for trailing commas, single quotes, or unquoted keys, which are not valid JSON.
Common questions
Does sorting change my data?
No. Sorting only reorders the keys within objects. Every value, every array, and every nested structure keeps the exact same content. The parsed data is identical, just written in a different key order.
Are nested objects sorted too?
Yes. The sort is recursive. Objects inside objects and objects inside array elements all get their keys sorted. Only array order itself is left untouched.
Why are some uppercase keys grouped separately?
Key comparison uses locale rules, so the exact placement of uppercase versus lowercase keys depends on your browser locale. The order is consistent for the same input, which is what matters for diffs and comparisons.
Can I sort a top-level array?
You can paste a top-level array. The array elements stay in place, while any objects inside them have their keys sorted. The array itself is never reordered.
100% private