Data Formats & Debugging
Canonical JSON for Signing and Hashing
Learn how canonical JSON gives signatures and hashes consistent input, including key ordering, number formatting and Unicode handling.

A signed JSON document needs a byte-exact representation. Pretty-printing and “stable enough” key order are not the same as a formal canonicalization rule.
This matters any time you hash or sign JSON and expect another system to reproduce the exact same bytes later.
In brief
What it is: Canonical JSON is a deterministic serialization of JSON data so that logically equivalent values produce the same byte representation for hashing and signing.
Why it matters: Without canonicalization, harmless formatting differences can invalidate signatures or change digests.
Worth knowing: A complete canonicalization standard also defines number, string, Unicode, and serialization details that affect the final bytes.
Canonical bytes need a defined standard
Many teams invent a house style for serialized JSON and assume that style is enough for signatures. It often is not. Number formatting, escaping, Unicode handling, and property ordering all influence the final bytes. A canonicalization scheme exists to pin those decisions down.
Use a specification instead of a convention.
What canonicalization buys you
Canonicalization makes JSON hashable and signable across systems that agree on the same scheme. It reduces arguments about whitespace and indentation because those cease to matter once the canonical output is derived. That does not remove the need for schema validation or business rules, but it removes one entire class of transport-level disagreement.
Produce the exact canonical JSON bytes that you intend to hash or sign.
- Use canonicalization when signatures or digests must be reproducible.
- Use the same documented canonicalization rules across libraries before signing or hashing.
- Validate JSON first; canonicalization is not a repair tool for malformed input.
Where teams overcomplicate this
The job is not to make JSON beautiful. The job is to make it deterministic. Once you hold that line, the tool can stay focused and trustworthy.
See it in a small example
Notice: A human sees the same data either way. A hash function only sees bytes. Canonicalization is how you make “same data” become “same bytes” consistently.
{"b":2,"a":1}
What to verify
- Validate the JSON first.
- Canonicalize before hashing or signing.
- Compare canonical outputs, not raw pretty-printed files, when investigating digest mismatches.
Common questions
Is minified JSON the same as canonical JSON?
Minification removes unnecessary whitespace; canonicalization also fixes serialization details and ordering rules.
Do I need canonicalization for every JSON API?
It matters when exact bytes must be reproduced for hashing, signatures, or stable comparison.