JSON Lines / NDJSON
Convert between JSON arrays and NDJSON (newline-delimited JSON) format.
What is JSON Lines (NDJSON)?
JSON Lines (also called NDJSON — Newline Delimited JSON, or JSONL) is a format where each line of a text file is a valid, independent JSON object. Unlike a regular JSON array that wraps all items in [ ], NDJSON has no wrapper — just one JSON object per line, separated by newline characters.
NDJSON is optimized for streaming and appending. You can process a 10 GB log file line-by-line without loading the whole thing into memory. You can append new records by simply adding a line — unlike a JSON array where adding a record requires rewriting the closing bracket. This makes NDJSON the standard format for log pipelines, BigQuery imports, Apache Kafka, and Elasticsearch bulk indexing.
JSON vs NDJSON
| Feature | JSON Array | NDJSON / JSON Lines |
|---|---|---|
| Structure | [ {...}, {...} ] | One JSON object per line |
| File extension | .json | .ndjson or .jsonl |
| Streamable | No — must parse entire file | Yes — process line by line |
| Appendable | No — breaks JSON syntax | Yes — just append a line |
| Memory use | Loads entire file | Constant — one line at a time |
| Common use | REST API responses | Logs, BigQuery, Kafka, Spark |
Reading NDJSON in Node.js
import { createReadStream } from 'fs';
import { createInterface } from 'readline';
const rl = createInterface({ input: createReadStream('data.ndjson') });
for await (const line of rl) {
if (line.trim()) {
const record = JSON.parse(line);
console.log(record.id);
}
}Reading NDJSON in Python
import json
with open('data.ndjson') as f:
for line in f:
if line.strip():
record = json.loads(line)
print(record['id'])