How to sort JSON keys
- Paste JSON or press Open file.
- Press Sort keys or Ctrl + Enter. The result uses your Indent setting.
Example
A config in no particular order:
{"timeout": 30, "Retries": 2, "endpoints": {"primary": "https://api.example.com", "backup": "https://api-eu.example.com"}, "allowedOrigins": ["https://shop.example", "https://admin.example"], "_comment": "staging", "2fa": true, "item10": 1, "item2": 2}
After Sort keys:
{
"2fa": true,
"Retries": 2,
"_comment": "staging",
"allowedOrigins": [
"https://shop.example",
"https://admin.example"
],
"endpoints": {
"backup": "https://api-eu.example.com",
"primary": "https://api.example.com"
},
"item10": 1,
"item2": 2,
"timeout": 30
}
endpoints is sorted inside too, while allowedOrigins keeps shop before admin: array order carries meaning (steps, rankings), so items never move. Objects inside arrays still get their keys sorted.
The sort order
Keys are compared by Unicode value, character by character, as JavaScript sorts strings by default:
- digits before letters:
2fais first - uppercase before lowercase:
RetriesbeforeallowedOrigins _between the two cases- no number reading:
item10beforeitem2
Python’s json.dumps(data, sort_keys=True) gives the same order for everyday keys, so a Python script will not reorder a file sorted here.
Keys that look like numbers
"10" sorts before "2" here because both are text. Load the result into a JavaScript object and stringify it, and JavaScript moves integer-like keys to the front in numeric order, "2" before "10". Python keeps the order it reads.
Why sort keys
Two exports of the same data often order keys differently, which makes git diffs noisy; once both are sorted, only real changes remain. JSON diff can also ignore key order by itself. Sorting also puts duplicate keys side by side, and the JSON validator flags them.
Skip it where people expect a set order, such as name and version at the top of a package.json. For indentation without reordering, use JSON pretty print.