JSON vs NDJSON: Logs, Streaming, and Line-Delimited Data
Last updated:
NDJSON (Newline-Delimited JSON) stores 1 complete JSON value per line, separated by \n, while standard JSON is a single document — that single structural rule determines which format to use. NDJSON lets you process a 10 GB log file in under 100 MB of RAM by reading line-by-line; standard JSON requires loading the entire document before the first record can be read, which fails past ~1 GB on most servers. NDJSON has 3 common names: NDJSON, JSON Lines (.jsonl), and LDJSON — all are wire-compatible. This guide covers the syntax difference between the 2 formats, streaming NDJSON in Node.js and Python, appending to log files safely, jq and command-line processing, and a decision checklist for choosing between them.
Example difference
[
{ "event": "login", "userId": 1 },
{ "event": "logout", "userId": 1 }
]{ "event": "login", "userId": 1 }
{ "event": "logout", "userId": 1 }The first block is one JSON array. The second block is NDJSON: each line can be parsed independently with JSON.parse(line).
Quick comparison
| Feature | JSON | NDJSON |
|---|---|---|
| Document shape | One value, often an object or array | One JSON value per line |
| Append new record | Requires editing the whole array | Append a line |
| Stream processing | Needs full document or streaming parser | Natural line-by-line parsing |
| Validation | Validate the whole document | Validate each line as a record |
| Best fit | APIs, config, structured files | Logs, exports, queues, analytics events |
When to use JSON
- API responses that return one coherent resource or collection.
- Config files where the entire document is loaded together.
- Nested data that belongs to one top-level object.
- Browser tools where users expect a single valid JSON document.
When to use NDJSON
- Application logs where each event is independent.
- Large exports that should be processed without loading everything into memory.
- Streaming APIs that send records as they become available.
- Batch imports where one bad record should not discard the whole file.
Parsing pattern
For NDJSON, split on newlines and parse each non-empty line. For a normal JSON array, parse once and then iterate over the array.
const records = ndjson
.split('\n')
.filter(Boolean)
.map((line) => JSON.parse(line))If parsing fails, the line number tells you which record is malformed. See the JSON.parse error guide for common syntax failures.
Validate sample records
Paste one NDJSON line or a converted JSON array into Jsonic's JSON Formatter to check syntax before writing an import or streaming parser.
Open JSON FormatterFrequently asked questions
What is NDJSON (Newline Delimited JSON)?
NDJSON stores one complete JSON value per line, separated by newlines. Each line can be parsed independently with JSON.parse. Also called JSON Lines (.jsonl) or LDJSON. It is used for logs, analytics events, and database exports that process records one at a time.
Can I stream NDJSON but not JSON?
Yes. NDJSON lines are self-contained, so a reader processes each as it arrives. Standard JSON requires the full document before parsing. Streaming JSON parsers exist (JSONStream, ijson) but are more complex than simple NDJSON line splitting.
What is the difference between JSON Lines and NDJSON?
JSON Lines (.jsonl) and NDJSON are the same format with different names. LDJSON is a third name. All store one JSON value per line. Files produced under any name are fully compatible with parsers expecting any other name.
Which is better for log files: JSON or NDJSON?
NDJSON is much better. Append a new log event by adding a line — no array bracket or comma editing needed. If the app crashes mid-write, only the last incomplete line is corrupted. Tools like jq, Logstash, and Vector read NDJSON line-by-line natively.
Can a standard JSON parser read NDJSON directly?
Not as a whole file — multi-line NDJSON is not valid JSON. Split on newlines and call JSON.parse per line: text.split("\n").filter(Boolean).map(JSON.parse). Libraries like jsonlines (Python) and jq also handle NDJSON files directly.
When does NDJSON break?
NDJSON breaks if a string value contains a literal unescaped newline character, since that splits one value across two lines. Proper JSON serializers always escape newlines as \n. Also filter empty lines before parsing and avoid byte-order marks at the file start.
Recommended reading
- Designing Data-Intensive Applications (2nd Edition) — Martin Kleppmann & Chris RiccominiThe modern classic on data systems — encoding formats, schemas, replication, and stream processing.
- JavaScript: The Definitive Guide (7th Edition) — David FlanaganThe complete reference for the language JSON came from — serialization, async, and the full standard library.
As an Amazon Associate, Jsonic earns from qualifying purchases.