PythiloLesson 4 of 8 · progress kept in this browser

Compress a JSON payload reproducibly

Compression operates on bytes, while JSON serialization starts with Python values. Keeping those stages separate makes a data pipeline easier to inspect: serialize the object, encode the text, compress the bytes, then reverse the steps when reading.

Gzip can include a timestamp in its header. Supplying a fixed mtime makes repeated compression of the same serialized data reproducible in this runtime. Stable JSON key ordering removes another avoidable difference. A compressed payload is still a format contract: malformed gzip or malformed decoded JSON should produce an error rather than an unrelated empty result.

import gzip, json
raw = json.dumps({"count": 3}).encode()
packed = gzip.compress(raw, mtime=0)
restored = json.loads(gzip.decompress(packed))
print(restored)
Your task

Implement pack_json(value) with sorted JSON keys, UTF-8 and gzip mtime=0. unpack_json(blob) reverses it. Equal dictionaries with different insertion orders must produce equal bytes in this runtime.