compare approaches one problem · two ways
Compare Approaches
The same problem solved different ways in Python, run on the same input, with what really differs and when to use each.
One JSON document vs JSON Lines
Count complaints per product from the same 600 complaints stored two ways.
what differs
A JSON document must be parsed whole before you can use any part; here the records sit three levels down inside the API envelope. JSON Lines puts one complete JSON value on each line, so a file can be read, written and split line by line.
at a glance
| approach | use it when |
|---|---|
| A · JSON response (one document) | API responses and small documents with nested structure and metadata. |
| B · JSON Lines (one record per line) | Logs, exports and streams: append records, process them one at a time, split files anywhere. |
Watch out: A JSONL file must not pretty-print records across lines. A truncated JSON document is unreadable; a truncated JSONL file loses only its last line.
the outputs
Each approach runs on the same input.
concepts
JSONJSON Linesstreaming
learn it
Write Python values as JSON · JSON Lines: one record per line
related comparisons
Nested dicts vs dataclass models · List vs generator: using it twice