Sort JSON keys for stable diffs & hashing
Alphabetize object keys recursively. Deterministic output is essential for diffs, signatures, JWT/JSON-LD canonicalization, and Merkle hashing.
{
"active": true,
"age": 36,
"id": 1,
"name": "Ada",
"roles": [
"admin",
"founder"
]
}How to sort JSON object keys
JSON objects are formally unordered, so any key ordering is valid. Sorting alphabetically gives a canonical form that's byte-stable across producers and consumers — critical for signatures, diffs, and hashing.
- 01
Paste JSON
Drop any valid JSON. Arrays' order is preserved (only object keys re-sort).
Inputs parse through the native
JSON.parsereviver chain — strict RFC 8259. Any syntax error returns line and column for the exact offending byte.objects onlyRFC 8259JSON.parsestrictliveinput · raw.json · RFC 8259{"name":"Ada","id":1}loaded · parsed RFC 8259RFC 8259 · ready - 02
Pick A→Z or Z→A
Ascending is the standard canonical order. Descending is useful for niche tooling that prefers reverse.
Indent options run through
JSON.stringify(value, null, n); the second argument lets you sort, transform, or omit keys via a replacer if you ever wire one in.ascdescindentJSON.stringifyliveorder · optionsasc · live{ "id": 1, "name": "Ada" }options applied · livelive - 03
Recursive vs top-level
Recursive (default) sorts every nested object too. Disable to sort only the root.
Large documents stream through a
useMemowith stable hashing so options re-apply instantly — no full re-parse if only formatting changes.recursivememoisedinstantdiff-friendlydeterministicdepth · rendereddeep · ready{ "user": {"age":36,"name":"Ada"} }rendered · livediff-friendly - 04
Hash / sign / commit
Now the JSON is byte-stable. Pipe to a hash function, sign with HMAC, or commit to git for clean diffs.
hashsigndiffdownstreamreadysha256(canonical) = deterministic
shippedsuccess
Production-grade JSON, the right way
Stick to RFC 8259
Strict JSON has no NaN, Infinity, comments, or trailing commas. If you need those, look at JSON5 or JSON Repair.
Preserve key order for diffs
JavaScript preserves insertion order, but two parsers shouldn’t be assumed equal. Use Sort JSON Keys before diffing or hashing.
Big integers lose precision
JS numbers lose precision past 2^53−1. Transport large IDs as strings or use BigInt.
Escape inside HTML
When inlining JSON inside <script>, escape </ to <\/ so HTML parsing doesn’t close your tag early.
Validate at boundaries
Trust the wire as little as possible. Run incoming JSON through a schema (Zod / Pydantic) before it touches business logic.
Minify in production
Minify removes whitespace; gzip removes redundancy. Together they typically shrink JSON payloads 70–85% over the wire.
When to canonicalize JSON
Anywhere byte-stable JSON matters.
Signature payloads
JWT, webhook signatures, HMAC of payloads — all require a canonical form. Output round-trips losslessly through JSON.parse + JSON.stringify, so the file you save is byte-identical to what you see. Streaming parsers, jq pipelines, and Postgres jsonb columns all accept the result without complaint.
10× faster to scanContent addressing
IPFS, Merkle trees, content-addressed stores hash JSON — sort first. Pairs perfectly with sort-keys + diff for review-friendly fixtures in CI. Whitespace-only churn disappears and reviewers see real changes, which keeps PR comments focused on logic rather than indentation.
cleaner PR diffsStable PR diffs
Sort fixtures before committing so PR diffs show real changes. Handles emoji and non-ASCII via UTF-8 throughout — your data survives copy-paste between editors, terminals, and IDEs. Combining marks and ZWJ sequences come out the other side intact.
RFC 8259 validJSON-LD canonicalization
RDF/JSON-LD signing standards require canonical JSON. Re-parses on every keystroke so syntax errors are caught at the exact character. Strict RFC 8259 — no trailing commas, no comments, no unquoted keys — keeps downstream parsers happy.
round-trip safeCache keys
Build cache keys from canonical JSON so two equivalent docs hit the same cache entry. Streams large documents without a server round-trip — privacy stays in the tab. Heavy parsing runs through useMemo so even 5 MB payloads keep the editor responsive.
schema-friendlySnapshot tests
Stable JSON snapshots make test assertions reliable across CI runs. Output is byte-stable: same input plus same options always produces identical bytes, ideal for content-addressed caches and snapshot tests. Downloads ship with the application/json MIME so editors apply syntax highlighting.
production-readyFrequently asked questions
Something missing?
Tell us what you need. We ship the top requests every two weeks.