JSON Key Sorter
Recursively sort all JSON object keys alphabetically. Arrays preserve their original order.
Did this tool solve your problem?
Code Examples
JavaScript
// Recursive JSON key sort
function sortKeys(value, order = 'asc') {
if (Array.isArray(value))
return value.map(v => sortKeys(v, order));
if (value !== null && typeof value === 'object') {
const keys = Object.keys(value).sort(
order === 'asc'
? (a, b) => a.localeCompare(b)
: (a, b) => b.localeCompare(a)
);
return Object.fromEntries(
keys.map(k => [k, sortKeys(value[k], order)])
);
}
return value;
}
const sorted = JSON.stringify(
sortKeys({ z: 3, a: 1, m: 2 }),
null, 2
);
// { "a": 1, "m": 2, "z": 3 }Python
import json
data = {"z": 3, "a": 1, "m": 2, "nested": {"y": 9, "b": 2}}
# Built-in sort_keys option
sorted_json = json.dumps(data, sort_keys=True, indent=2)
print(sorted_json)
# {
# "a": 1,
# "m": 2,
# "nested": { "b": 2, "y": 9 },
# "z": 3
# }
# Reverse / descending sort
def sort_keys_desc(obj):
if isinstance(obj, dict):
return {k: sort_keys_desc(obj[k])
for k in sorted(obj, reverse=True)}
if isinstance(obj, list):
return [sort_keys_desc(v) for v in obj]
return objjq (CLI)
# Sort all keys recursively (ascending)
jq 'walk(if type == "object"
then to_entries | sort_by(.key) | from_entries
else . end)' input.json
# One-liner for simple objects
echo '{"z":3,"a":1}' | jq -S .
# -S flag sorts keys alphabetically
# Sort and save to file
jq -S . input.json > sorted.jsonGo
package main
import (
"encoding/json"
"sort"
)
// JSON marshaling in Go preserves map key
// order alphabetically by default.
func SortKeys(v interface{}) interface{} {
switch val := v.(type) {
case map[string]interface{}:
keys := make([]string, 0, len(val))
for k := range val { keys = append(keys, k) }
sort.Strings(keys)
result := make(map[string]interface{})
for _, k := range keys {
result[k] = SortKeys(val[k])
}
return result
case []interface{}:
for i, item := range val {
val[i] = SortKeys(item)
}
}
return v
}
// json.Marshal on map[string]interface{}
// already sorts keys alphabetically.
b, _ := json.MarshalIndent(data, "", " ")Frequently Asked Questions
Why would I want to sort JSON keys?
Sorting keys makes JSON easier to compare with diff tools — when two JSON objects have the same data but different key order, a diff would show false changes. It also helps with canonical serialization, standardizing config files, and making large JSON objects easier to navigate visually.
Does it sort nested objects too?
Yes. The sorter recursively processes all nested objects at any depth. Arrays are preserved in their original order — only object keys are sorted.
What happens to arrays in the JSON?
Array elements are kept in their original order because arrays are ordered by definition. If an array contains objects, the keys within each object are sorted, but the array order itself is unchanged.
Is there a size limit for the JSON input?
There's no explicit limit, but very large JSON files (multiple MB) may be slow to process in the browser. For large files, consider using a command-line tool like jq: jq 'keys_unsorted | sort' file.json
How can I sort JSON keys from the command line?
Use jq with the to_entries | sort_by(.key) | from_entries filter, or Python's json.dumps with sort_keys=True. See the code examples below.
Does sorting keys change the meaning of my JSON?
No. JSON objects are defined as unordered collections of key-value pairs, so key order has no semantic meaning. Any JSON parser will produce identical results regardless of key order.