What Is JSON Lines?
JSON Lines is a text format where every line is one complete JSON value, usually an object. Lines are separated by a newline. The file extension is .jsonl. NDJSON (newline-delimited JSON, .ndjson) is practically the same format.
{"id": 1, "name": "Emily Clark", "city": "London"}
{"id": 2, "name": "James Smith", "city": "Manchester"}
{"id": 3, "name": "Olivia Brown", "city": "Bristol"}The same data as a normal JSON array would look like this:
[
{"id": 1, "name": "Emily Clark", "city": "London"},
{"id": 2, "name": "James Smith", "city": "Manchester"},
{"id": 3, "name": "Olivia Brown", "city": "Bristol"}
]Why Use JSON Lines?
- Streaming: you can read and process one record at a time, without loading the whole file.
- Appending: add new records by writing new lines; no need to rewrite brackets.
- Robust: one broken line does not make the whole file unreadable.
- Tools love it: logs, BigQuery, Elasticsearch bulk imports, data pipelines and AI training datasets often use JSONL.
Rules
- Each line must be valid JSON on its own. No pretty printing across lines.
- Use UTF-8 and a newline (
\n) between records. - No commas between lines and no surrounding [ ].
- Empty lines are best avoided; many readers skip them.
Read and Write JSONL in Python
import json
# Write
users = [{"id": 1, "name": "Emily"}, {"id": 2, "name": "James"}]
with open("users.jsonl", "w", encoding="utf-8") as f:
for user in users:
f.write(json.dumps(user, ensure_ascii=False) + "\n")
# Read, one line at a time
with open("users.jsonl", encoding="utf-8") as f:
for line in f:
if line.strip():
user = json.loads(line)
print(user["name"])Find the Broken Line in a Big JSONL File
One bad line stops the whole loop with an error like Expecting value: line 1 column 14. Because every line is parsed on its own, "line 1" there means the first line of that record, not of the file. Count the lines yourself to find the real one, and keep going instead of crashing:
import json
with open("questions.jsonl", encoding="utf-8") as f:
for number, line in enumerate(f, start=1):
if not line.strip():
continue # skip empty lines
try:
item = json.loads(line)
except json.JSONDecodeError as error:
print(f"Line {number} is not valid JSON: {error}")
continue
print(item["question"])If the third line of the file is {"question": broken} (the text is missing its quotes), this prints the good records and Line 3 is not valid JSON: Expecting value: line 1 column 14 (char 13). Paste the bad line into the JSON Validator to see exactly what is wrong with it.
Read JSONL in JavaScript
const text = await (await fetch('/users.jsonl')).text()
const users = text
.split('\n')
.filter((line) => line.trim())
.map((line) => JSON.parse(line))Convert JSONL to a JSON Array
Parse every line and collect the results in a list, as shown above, then save the list with json.dump or JSON.stringify. To check a single line, paste it into our JSON Validator.
Convert JSON Lines to CSV
import pandas as pd
df = pd.read_json("events.jsonl", lines=True)
df.to_csv("events.csv", index=False)lines=True tells pandas to read one JSON object per line. Without it, pandas reports "Trailing data". More examples are in How to Convert JSON to CSV in Python.
Frequently Asked Questions
What is the difference between JSON Lines and NDJSON?
They are almost the same: one JSON value per line, separated by newline characters. JSON Lines uses the .jsonl extension, NDJSON often uses .ndjson.
Why use JSON Lines instead of a JSON array?
Each line can be read, written and processed on its own. That makes JSON Lines great for logs, big data exports and streaming, because you never need the whole file in memory.
Can a JSON Lines file have pretty printed JSON?
No. Every record must be on a single line.
How do I convert JSON Lines to a JSON array?
Read the file line by line, parse each non-empty line with JSON.parse or json.loads, and collect the results in a list.