Inferensys

Glossary

Canonical JSON

A deterministic serialization format for JSON data that ensures the same logical object always produces the exact same byte sequence, which is a prerequisite for consistent cryptographic hashing.
Data scientist building training data pipeline on laptop, data preprocessing visible, technical workspace.
DETERMINISTIC SERIALIZATION

What is Canonical JSON?

Canonical JSON defines a strict set of rules for serializing JSON data to guarantee that a single logical object always produces an identical, byte-for-byte sequence, a critical prerequisite for consistent cryptographic hashing and digital signing.

Canonical JSON is a deterministic serialization format that eliminates the syntactic flexibility of standard JSON to produce a single, unambiguous byte representation for any given data structure. By enforcing strict rules—such as sorted object keys, no insignificant whitespace, and a specific Unicode normalization form—it ensures that two systems independently serializing the same logical payload will generate the exact same sequence of bytes, enabling reliable content-addressable storage.

This format is foundational for tamper-proof model registries and software supply chain security, where a model's integrity is verified by comparing its cryptographic hash. Without canonicalization, semantically identical JSON documents could produce different hashes due to key ordering or whitespace variations, breaking **attestation** chains and **non-repudiation** guarantees in frameworks like **in-toto** and **Sigstore**.

DETERMINISTIC SERIALIZATION

Key Features of Canonical JSON

Canonical JSON (JCS) defines a strict set of rules for serializing JSON data to ensure that logically identical payloads always produce an identical sequence of bytes—a critical prerequisite for consistent cryptographic hashing and digital signatures.

01

Deterministic Key Ordering

Object keys are serialized in lexicographic order based on their UTF-16 code unit values. This eliminates the non-determinism inherent in standard JSON, where key order is arbitrary.

  • Keys are sorted by comparing Unicode code points
  • Nested objects are recursively sorted
  • Ensures {"b":2,"a":1} and {"a":1,"b":2} produce identical byte sequences
02

Whitespace Elimination

All insignificant whitespace is stripped from the output. No spaces, tabs, newlines, or carriage returns appear outside of string literals.

  • No indentation or pretty-printing
  • No trailing whitespace after commas or colons
  • Produces the most compact valid JSON representation
03

Unicode Normalization

String values are normalized using Unicode Normalization Form C (NFC) before serialization. This ensures that characters composed of combining marks are represented consistently.

  • Prevents hash mismatches from equivalent Unicode representations
  • Applies to both keys and string values
  • Critical for internationalized data integrity
04

Number Serialization Rules

Numbers are serialized with no leading zeros, no trailing decimal points, and using E notation for exponents. The integer 1.0 becomes 1, and 1E+2 becomes 100.

  • No exponential notation unless required for magnitude
  • No + sign on positive exponents
  • Ensures 1.0, 1e0, and 1 all canonicalize to 1
05

String Escape Consistency

Characters are escaped using the minimum required escape sequence. Control characters use uppercase hex in \u notation, and the solidus / is never escaped.

  • \b, \f, \n, \r, \t for standard control characters
  • \u00XX for other control characters (uppercase hex)
  • Double-quote and backslash always escaped
06

Cryptographic Hashing Foundation

Canonical JSON is the canonicalization scheme used by RFC 8785 (JCS). It enables reliable digital signatures and content hashing where byte-level determinism is mandatory.

  • Used in Verifiable Credentials and DID Documents
  • Enables content-addressable storage for JSON payloads
  • Foundation for tamper-evident data structures
SERIALIZATION FORMAT COMPARISON

Canonical JSON vs. Standard JSON vs. JCS

A technical comparison of deterministic JSON serialization approaches for cryptographic hashing and data integrity verification.

FeatureStandard JSON (RFC 8259)Canonical JSON (JCS)OLPC Canonical JSON

Deterministic output

Key ordering

Arbitrary

Lexicographic by UTF-16 code unit

Lexicographic by UTF-8 byte value

Whitespace normalization

Unicode escape handling

Optional

Lowercase hex, shortest form required

Lowercase hex, shortest form required

Number serialization

Implementation-dependent

No exponential notation, no leading zeros, no trailing zeros

JSON number grammar, no exponential notation

UTF-8 encoding required

Trailing comma prohibition

Duplicate key handling

Undefined

Prohibited

Prohibited

Specification status

IETF Standard

RFC 8785

Informal specification

CANONICAL JSON

Frequently Asked Questions

Clear answers to the most common questions about deterministic JSON serialization for cryptographic integrity.

Canonical JSON is a deterministic serialization format that ensures a given logical JSON object always produces the exact same sequence of bytes, regardless of the encoder or programming language used. It works by enforcing a strict set of rules on the serialization process: object keys must be sorted lexicographically, whitespace outside string literals is eliminated, number formatting is normalized, and Unicode escape sequences are standardized. This byte-level determinism is a prerequisite for cryptographic hashing and digital signatures, where even a single bit of difference in serialization would produce a completely different hash value. The most widely adopted specification is the JSON Canonicalization Scheme (JCS), defined in RFC 8785, which provides a precise algorithm for transforming any valid JSON value into its canonical form.

Prasad Kumkar

About the author

Prasad Kumkar

CEO & MD, Inference Systems

Prasad Kumkar is the CEO & MD of Inference Systems and writes about AI systems architecture, LLM infrastructure, model serving, evaluation, and production deployment. Over 5+ years, he has worked across computer vision models, L5 autonomous vehicle systems, and LLM research, with a focus on taking complex AI ideas into real-world engineering systems.

His work and writing cover AI systems, large language models, AI agents, multimodal systems, autonomous systems, inference optimization, RAG, evaluation, and production AI engineering.