skillfed

canonicaljson

Canonical JSON

canonicaljson v2.0.0 272.6K downloads/30d#8,208 on PyPI30
Permissive license Apache License, Version 2.0 Active released

What it is and what it does

Canonicaljson produces deterministic JSON output by enforcing a canonical form: object keys are always sorted, whitespace is eliminated, and character escaping follows RFC 7159 with minimal sequences. This makes the encoded output identical every time for the same input, which is essential for cryptographic signing, content hashing, and any system where reproducible serialization matters.

The package wraps either simplejson (by default on CPython) or the standard library json module (on PyPy) and adds a preserialisation hook system to handle custom types. It requires Python 3.7 or newer and has no external runtime dependencies, making it lightweight to integrate into existing projects.

Use it for:

  • Signing JSON data cryptographically: canonical form ensures signatures remain valid across different serialization libraries.
  • Deduplicating or caching JSON objects: identical input always produces identical bytes, enabling content-addressed storage.
  • Blockchain or ledger applications: deterministic encoding is critical for consensus and transaction verification.
  • API request/response logging: canonical form makes log entries comparable and reduces storage by eliminating whitespace.
  • Testing JSON-producing code: canonical output makes assertions on serialized data deterministic and independent of key order.

Worth the install?

AI-flagged interpretation of the facts on this page — verify before relying

Encodes Python objects to canonical JSON with sorted keys, minimal whitespace, and consistent escaping, ensuring identical output for the same input across runs.

Yes. Canonicaljson solves a real problem—deterministic JSON serialization—that the standard library does not address. It has no dependencies, low install friction, active maintenance, a permissive license, and no known vulnerabilities. Install it if your application requires reproducible JSON encoding for signing, hashing, or comparison; skip it if you only need standard JSON serialization.

Install

canonicaljson on PyPI

pip

pip install canonicaljson

uv

uv add canonicaljson

poetry

poetry add canonicaljson

Installing canonicaljson

Before you install

Low friction: pure Python wheel with no runtime dependencies. Actively maintained as of 2026-05-06 with production-stable status since its 2015 release.

License in practice

Apache License 2.0 (permissive) places no significant restrictions on use, modification, or distribution in proprietary or open-source projects.

Quickstart

pip install canonicaljson

import canonicaljson
result = canonicaljson.encode_canonical_json({"key": "value"})
print(result)  # b'{"key":"value"}'

Requires Python 3.7 or newer.

Verify before relying

  • Performance characteristics compared to standard library json for large objects.
  • Whether the preserialisation callback mechanism covers all custom type use cases in practice.

Package facts

License Apache License, Version 2.0 (permissive)
Python support supports the current Python release (>=3.7)
Install friction low — pure-Python wheel
Runtime dependencies none
Maintenance actively maintained — 1,248 days since the last release
Last repo commit
First released
Downloads 272,563/month — #8,208 on PyPI (30-day window, as of 2026-08-14)
Known vulnerabilities none known (OSV.dev, checked 2026-08-14)

Evidence: canonicaljson-2.0.0-py3-none-any.whl

Keywords: json

Development Status :: 5 - Production/StableIntended Audience :: DevelopersLicense :: OSI Approved :: Apache Software LicenseProgramming Language :: Python :: 2Programming Language :: Python :: 3

Tags

canonical json encodingdeterministic json serializationsorted keys jsonminimal whitespace jsonrfc 7159 json encoderconsistent json outputjson canonicalization
json-serializationcryptography-adjacentdeterministic-output

More Software Development packages