Formatter · 4 min read
How to Sort JSON Keys Alphabetically
Unsorted JSON keys turn every diff into noise. A JSON minifier with a sort option recursively alphabetizes every object key at every nesting level — and crucially, leaves array order untouched.
1. Why Sort JSON Keys
Two practical reasons come up over and over:
- Stable diffs. When two developers edit the same config, key reordering shows up as a 200-line diff that hides the real 3-line change. Sorted keys eliminate that.
- Faster review. Reviewers find the field they need by glancing at the alphabet rather than scanning the whole file.
- Deterministic output. Tests, snapshots, and golden files become byte-identical instead of "almost identical."
2. Ascending or Descending
Most projects use ascending (A to Z) as a convention, but the choice should match your team's habit:
- Ascending (A → Z). Default. Puts fields in reading order. Matches the convention used by most config files (eslint, tsconfig, package.json).
- Descending (Z → A). Useful for placing recently added or priority fields at the top of an object.
- Case sensitivity. Decide whether
Zebrasorts beforeapple(ASCII order) or after (case-insensitive). Most JSON sorters use case-insensitive comparison by default.
3. The Critical Rule: Sort Objects, Not Arrays
A correct JSON sorter reorders keys inside objects but never reorders elements inside arrays. The two data structures carry different meanings:
- Object keys are a set. Order does not affect the data —
{"a":1,"b":2}and{"b":2,"a":1}are semantically identical. Sort freely. - Array elements are an ordered list. Position matters —
[1,2,3]is not the same as[3,1,2]. Never sort inside an array.
A sorter that "sorts everything" will silently break your data. Always confirm that the tool you use only reorders object keys.
4. Sort as You Type with Real-Time Sorting
A live sorter re-sorts the output every time you edit the source. This removes the click-sort-click cycle:
- Paste messy JSON. The sorted version appears instantly on the right.
- Add or remove a key. The new key slots into the correct alphabetical position automatically.
- Compare original vs sorted. Side-by-side preview shows what changed, which is useful for verifying before commit.
Sort and Minify JSON in One Click
Paste JSON, choose ascending or descending, get a sorted and minified version instantly. Free, browser-based, no uploads.
Open JSON Minifier →Best Practices for Sorted JSON
- Pick a team convention. Document whether you sort ascending or descending, and whether the comparison is case-sensitive. Inconsistency defeats the point.
- Automate with pre-commit hooks. A tool like
prettierwith--sort-keysenforces sort order on every commit. Manual sorting drifts. - Use sorted output for golden files. Snapshot tests are far more reliable when keys are in deterministic order — every test run produces identical bytes.
- Combine with minify. For storage and transmission, sort first, then minify. The result is smaller, more diff-friendly, and more cacheable.
- Validate before sorting. Run the JSON through a validator first if you suspect syntax errors — invalid input cannot be sorted.
Frequently Asked Questions
How do I sort JSON keys alphabetically?
Paste your JSON into a sorter and click the sort button. The tool recursively alphabetizes every object key at every nesting level. Pick ascending (A to Z) or descending (Z to A) order.
Does JSON sort change array order?
No. A correct sorter only reorders keys inside objects. Array element order is preserved because it carries semantic meaning — the first element is not equivalent to the second.
Can I sort nested JSON objects?
Yes. A recursive sort handles every level of nesting. A nested object inside an array inside an object is sorted at every level it appears.
Why sort JSON keys at all?
Sorted keys produce stable diffs in version control, make manual review faster, and reduce visual noise when comparing two JSON files. Unsorted keys make every commit look like a re-shuffle.
Is the JSON uploaded to a server for sorting?
No. A browser-based sorter runs entirely client-side. Your JSON is parsed and reordered in memory — the data never leaves your computer.
Looking for more guides? See the full JSONXX How To index.