Skip to content
ZeroServer.tools

Sort JSON Keys

Recursively sort all object keys alphabetically for consistent, diff-friendly JSON.

Total Keys
7
Max Depth
2
Lines
1014
Size
136B → 152B

About Sort JSON Keys

Sorting JSON keys alphabetically makes JSON objects deterministic and easier to diff. When two JSON objects have the same data but different key order, a plain text diff shows every line as changed. With sorted keys, only genuinely changed values appear in diffs. This tool performs a deep sort — nested objects at any depth are also sorted. Arrays are left in their original order since array position is semantically significant, but nested object keys inside arrays will be recursively sorted if enabled.

Built and maintained by Meet Shah · Last updated

What this tool is used for

  • Producing a canonical form so two documents can be diffed meaningfully.
  • Normalising a config before committing it, so the next diff is small.
  • Making a large object easier to scan by putting keys in a known order.
  • Comparing two API responses whose key order differs.
  • Producing a stable form for hashing or caching.

Frequently Asked Questions

Why sort JSON keys at all?
To make diffs meaningful. Two exports of the same configuration with keys in different orders produce a diff full of moves, hiding the one real change. Sorting canonicalises the order so a diff shows only what actually differs — the same reason lockfiles are sorted.
Does sorting change what the JSON means?
No. Object members are unordered by specification, so a parser treats sorted and unsorted forms as the same document. Arrays are a different matter — their order IS meaningful, so array elements are never reordered here, only the keys of objects inside them.
How are keys compared?
By code-unit order, which is what `Array.prototype.sort` does by default. That puts all uppercase letters before all lowercase — `Zebra` before `apple` — and orders non-ASCII by code point rather than alphabetically for any language. It is deterministic, which is what canonicalisation needs.
Does it sort nested objects too?
Yes, recursively, including objects inside arrays. A partial sort would defeat the purpose: the point is that the same data always produces byte-identical output, and a nested object left in its original order breaks that guarantee.
Is this the same as JSON canonicalisation?
Related but weaker. RFC 8785 (JCS) also specifies exact number formatting, string escaping and UTF-8 handling, because a canonical form is used for signatures where every byte matters. Sorting keys is the largest part of it and enough for diffing, not enough for hashing.

Common errors and gotchas

  • Sorting arrays as well as objects, which changes data rather than normalising it.
  • Assuming key order was meaningless, when some consumers do rely on it despite the specification.
  • Sorting with an ASCII comparison, which places uppercase keys before lowercase ones.
  • Losing a duplicate key silently, which a parser resolves before the sort ever runs.
  • Sorting a document a formatter will re-emit differently, producing churn on every save.

Related Developer Utilities tools

Private & free — this tool runs entirely in your browser.

IndieKitShip your Next.js startup in days.affiliate