Predict the output
Is the compressed JSON more than 10× smaller?
import json
import zlib
rows = [{"id": i, "status": "shipped", "country": "IN"}
for i in range(500)]
raw = json.dumps(rows).encode()
packed = zlib.compress(raw, 6)
print(len(raw) > 10 * len(packed))Hint 1 of 1
How repetitive is this JSON?
Choose an answer, then open Show answer below to check it.
Show answer
Answer: A True
Correct. Repeated keys and values compress very well.
DEFLATE replaces repeats with back-references. 500 rows with the same keys and almost the same values are nearly all repeats, so they shrink far more than 10×.
Why not the others?
- B
False. 500 near-identical rows are highly repetitive. - C An error. zlib.compress takes bytes, which
encode()gives. - D
None. The comparison returns a bool.