JSON Lines in code
jq -s '.' data.jsonl > data.json # JSONL -> JSON array
jq -c '.[]' data.json > data.jsonl # JSON array -> JSONL
jq -c 'select(.event == "login")' data.jsonl # filter records
import json
with open("data.jsonl") as f:
records = [json.loads(line) for line in f if line.strip()]
with open("out.jsonl", "w") as f:
for r in records:
f.write(json.dumps(r, ensure_ascii=False) + "\n")
# pandas
import pandas as pd
df = pd.read_json("data.jsonl", lines=True)
import { createReadStream } from 'node:fs';
import { createInterface } from 'node:readline';
for await (const line of createInterface({ input: createReadStream('data.jsonl') })) {
if (line.trim()) handle(JSON.parse(line));
}
Want to filter or reshape the records? The jq playground runs real jq. Need a spreadsheet? Convert the JSON array with JSON to CSV.
Frequently asked questions
What is JSON Lines (JSONL)?
A text format with one complete JSON value per line, usually an object. It’s also called NDJSON (newline-delimited JSON). Because each line stands alone, files can be appended to, streamed and processed line by line without loading everything, which is why it’s used for logs, data exports, BigQuery and Elasticsearch bulk loads, and machine-learning datasets such as OpenAI fine-tuning and batch files.
JSONL vs a JSON array: when should I use which?
Use JSON Lines for large or growing datasets and streams: you can read the first record without parsing the rest, and a broken line doesn’t ruin the file. Use a JSON array when a program expects a single JSON document, such as an API response, a config file or a test fixture.
What happens with an invalid line?
The conversion stops and the error shows the line number and the problem, for example a trailing comma or a missing quote, so you can fix it. Blank lines and a byte order mark at the start of the file are ignored, and Windows line endings are fine.
Can a JSONL record span several lines?
No. The JSON Lines format requires every record to be on one line; line breaks inside strings must be written as \n. Converting JSON to JSONL writes each record compactly on one line, which guarantees this.
Are large numbers safe?
Yes. Numbers are copied as written, so IDs like 12345678901234567890 and prices like 19.90 come out unchanged. A JSON.parse-based converter would round the ID and write 19.9.