JSON vs JSONL: differences and when to use each
JSON holds one document per file; JSONL (newline-delimited JSON) holds one JSON object per line. Same syntax per value, opposite trade-offs for streaming, logs, and large datasets.
JSON holds one document per file; JSONL (newline-delimited JSON) holds one JSON object per line. Same syntax per value, opposite trade-offs for streaming, logs, and large datasets.
A JSONL file is simply valid JSON values separated by newlines, with no enclosing array and no commas between lines. Each line parses independently, which changes everything about how the file can be produced and consumed.
// JSON: one array document
[
{ "id": 1, "ok": true },
{ "id": 2, "ok": false }
]// JSONL: one object per line
{ "id": 1, "ok": true }
{ "id": 2, "ok": false }| JSON | JSONL | |
|---|---|---|
| Structure | One value per file | One value per line |
| Streaming writes | Hard: needs the closing bracket | Trivial: append a line |
| Streaming reads | Needs an incremental parser | Read line by line |
| Partial corruption | Whole file fails | Only bad lines fail |
| Human editing | Formatter-friendly | Grep and sed friendly |
| Typical use | APIs, configs | Logs, datasets, exports |
JSONL to JSON: wrap the lines in [ ], join with commas. JSON array to JSONL: print each element on its own line. jq does both in one command: jq -c '.[]' array.json produces JSONL, and jq -s '.' lines.jsonl collects it back.
Note that jsonblocks tools expect standard JSON. To inspect JSONL here, convert it to an array first, or validate one line at a time.
JSON Lines, also called newline-delimited JSON (NDJSON). Each line is a self-contained JSON value, usually an object.
No. A multi-line JSONL file is not one JSON document, so JSON.parse rejects it. Parse line by line instead, or wrap the lines into an array.
Writers can append one line per event without reading or rewriting the file, and readers can stream it line by line. A corrupt line costs one event, not the whole file.