canonical · deterministic · signing-safe

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.

Sort JSON Keys Workspace
Input size
94 B
7 lines
Output size
106 B
canonical
Order
A → Z
recursive
Status
Valid
deterministic
Order
input · json
output · sorted
{
  "active": true,
  "age": 36,
  "id": 1,
  "name": "Ada",
  "roles": [
    "admin",
    "founder"
  ]
}
Quick Start

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.

  1. 01

    Paste JSON

    Drop any valid JSON. Arrays' order is preserved (only object keys re-sort).

    Inputs parse through the native JSON.parse reviver chain — strict RFC 8259. Any syntax error returns line and column for the exact offending byte.

    objects onlyRFC 8259JSON.parsestrictlive
    input · raw.json · RFC 8259
    {"name":"Ada","id":1}
    loaded · parsed RFC 8259RFC 8259 · ready
  2. 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.stringifylive
    order · optionsasc · live
    {
      "id": 1,
      "name": "Ada"
    }
    options applied · livelive
  3. 03

    Recursive vs top-level

    Recursive (default) sorts every nested object too. Disable to sort only the root.

    Large documents stream through a useMemo with stable hashing so options re-apply instantly — no full re-parse if only formatting changes.

    recursivememoisedinstantdiff-friendlydeterministic
    depth · rendereddeep · ready
    {
      "user": {"age":36,"name":"Ada"}
    }
    rendered · livediff-friendly
  4. 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.

    hashsigndiff
    downstreamready
    sha256(canonical) = deterministic
    shippedsuccess
Pro tips

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.

Use Cases

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 scan

Content 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 diffs

Stable 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 valid

JSON-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 safe

Cache 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-friendly

Snapshot 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-ready
FAQ

Frequently asked questions

No. JSON objects are unordered by spec — sorted output is byte-different but semantically identical.
No — array order is meaningful in JSON and is preserved exactly. Only object key order changes.
We use String.localeCompare which respects Unicode collation. For pure ASCII fields it matches sort order most languages expect.
No. All processing runs locally.
Yes — disable Recursive to leave nested objects untouched.
Suggest a tool

Something missing?

Tell us what you need. We ship the top requests every two weeks.

Sent straight to the maintainers · no account needed
47 in queue·8 shipped this month
Request queued. Thanks!