Canonical JSON sorts object keys recursively and emits minified JSON so the same logical document always produces identical text. Agents use this for cache keys, content hashes, and config diffs when key order would otherwise vary between serializers or runtimes.
How it works
The input is parsed with JSON.parse, then every object is walked depth-first. Keys at each level are sorted lexicographically, arrays keep their element order, and the result is serialized without extra whitespace. Running the tool twice on the same logical JSON yields byte-identical output, which is the basis for stable fingerprints and ETags.
When to use it
- Fingerprint JSON configs before and after merges
- Compare API responses ignoring key order
- Build stable ETags for JSON bodies in agent caches
- Normalize tool output before hashing or diffing in CI
Example
Input {"b":1,"a":{"d":2,"c":3}} becomes {"a":{"c":3,"d":2},"b":1}. Re-running the tool on that output returns the same string unchanged.
Limitations
Arrays keep their order; only object keys are sorted. Parsing follows standard JSON.parse rules—duplicate keys keep the last value, and non-JSON literals (undefined, NaN, comments) are rejected. Very large documents are held fully in browser memory.