Canonical JSON

2.0.0 · active · verified Thu Apr 16

Canonical JSON is a Python library designed to produce a deterministic, byte-for-byte consistent JSON serialization of Python data structures. This is crucial for applications requiring cryptographic hashing or signatures where the exact byte representation of JSON data must be consistent across different environments and executions. The library is currently at version 2.0.0 and maintains an active development and release cadence.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates basic usage of `encode_canonical_json` and how to register a preserialization callback for custom Python objects, a feature introduced in version 2.0.0 to handle types not natively supported by standard JSON serialization. Keys in objects are sorted, and whitespace is removed for canonical form.

import canonicaljson
from typing import Any, Dict

data = {"b": 2, "a": 1, "nested": {"y": 2, "x": 1}, "list": [3, 1, 2]}
canonical_bytes = canonicaljson.encode_canonical_json(data)
print(f"Canonical JSON: {canonical_bytes.decode('utf-8')}")
# Expected: {"a":1,"b":2,"list":[3,1,2],"nested":{"x":1,"y":2}}

# Example for custom types (v2.0.0+)
class CustomObject:
    def __init__(self, value):
        self.value = value

def custom_serializer(obj: CustomObject) -> Dict[str, Any]:
    return {"custom_value": obj.value, "_type": "CustomObject"}

canonicaljson.register_preserialisation_callback(CustomObject, custom_serializer)

custom_data = {"item": CustomObject("hello")}
custom_canonical_bytes = canonicaljson.encode_canonical_json(custom_data)
print(f"Canonical JSON with Custom Object: {custom_canonical_bytes.decode('utf-8')}")
# Expected: {"item":{"_type":"CustomObject","custom_value":"hello"}}

view raw JSON →