Dev Helpers

Python / Serialization

Encode sorted JSON

Dev Helpers · December 15, 2025

Sorting JSON keys makes snapshots easier to compare. Array order remains significant and is preserved.

Worked example

import json
print(json.dumps({'b':2,'a':1}, sort_keys=True))

Result

{"a": 1, "b": 2}

Implementation notes

Sorted keys alone do not define a complete canonical JSON encoding. Whitespace, number handling and Unicode escaping also affect byte-for-byte comparison.

Try it yourself

Serialize dictionaries built in different insertion orders and compare their arrays too.

Open the Python example →

Editorial date December 15, 2025 · Published December 6, 2026

Related articles

Run an async function

An async function call creates a coroutine. A top-level runner supplies the event loop that executes it.

Transpose a matrix

Transposing rows into columns assumes a decision about uneven rows. Ordinary zip stops at the shortest input.

Unpack with a remainder

Starred unpacking separates a fixed header from a variable-length tail. It still requires enough elements for the fixed positions.