canonicaljson
Canonical JSON
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 canonicaljsonuv
uv add canonicaljsonpoetry
poetry add canonicaljsonInstalling 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
Tags
More Software Development packages
Provides backported and experimental type hints…
permissive · top 100 on PyPI
numpyNumPy provides an N-dimensional array object…
permissive · top 100 on PyPI
fastapiFastAPI is a Python web framework for building…
permissive · top 100 on PyPI
annotated-docProvides a way to document function parameters,…
permissive · top 100 on PyPI
typerTyper builds command-line applications from…
permissive · top 1,000 on PyPI
distlibDistlib provides low-level packaging utilities…
permissive · top 1,000 on PyPI
jcsCanonicalizes JSON according to RFC 8785,…
permissive · top 5,000 on PyPI
rfc8785Implements RFC 8785 (JSON Canonicalization…
permissive · top 5,000 on PyPI
simplejsonsimplejson encodes Python objects to JSON and…
permissive · top 1,000 on PyPI
jsonseqEncodes and decodes JSON text sequences…
permissive · top 15,000 on PyPI
json-encoderProvides JSON serialization with…
unclear · top 15,000 on PyPI
canoserCanoser implements canonical serialization for…
permissive · top 15,000 on PyPI
risonEncodes and decodes data to and from Rison, a…
permissive · top 15,000 on PyPI
prisonEncodes and decodes a compact URI-friendly data…
permissive · top 5,000 on PyPI
numpyencoderProvides a custom JSON encoder class that…
permissive · top 15,000 on PyPI
demjson3demjson3 encodes, decodes, and validates JSON…
copyleft · top 15,000 on PyPI