Record Boundaries in JSON Lines
In JSON Lines each record is one complete JSON value on a single line, with LF as the record separator; a pretty-printed object spans multiple lines and is therefore not valid JSON Lines.
The short answer
JSON Lines, also called newline-delimited JSON or NDJSON, stores one complete JSON value per line and uses LF as the record separator. Each line must parse as a JSON value on its own, so a pretty-printed object spanning several lines is not valid JSON Lines. A line break inside a JSON string must be escaped as \n, which is why an unescaped LF can always be trusted as a record boundary.
A minimal example
A valid JSON Lines file:
{"id":1,"ok":true}
{"id":2,"tags":["a","b"]}
The same first record pretty-printed is valid JSON but not valid JSON Lines:
{
"id": 1,
"ok": true
}
To turn a pretty-printed JSON object into a single JSON Lines record, compact it:
jq -c . pretty.json > record.jsonl
Reading is line by line:
import json
with open("events.jsonl", encoding="utf-8") as f:
for number, line in enumerate(f, 1):
if not line.strip():
continue
record = json.loads(line)
print(number, record["id"])
Skipping blank lines is a convention; the specification says a blank line is not a valid value.
Why this happens
JSON itself allows insignificant whitespace, including space, tab, CR, and LF, between tokens. That is what makes pretty printing legal. JSON Lines adds a stricter rule: each line, without its terminator, must be a complete JSON value. The newline is therefore a record separator, not formatting.
JSON strings cannot contain raw control characters. A literal LF inside a string is invalid JSON; it must be written as the two-character escape \n. So an unescaped LF can only appear between tokens, exactly where a record boundary belongs. This lets line-oriented tools such as grep, wc -l, and awk process records without parsing JSON.
What changes the answer
| Detail | Rule |
|---|---|
| Encoding | UTF-8 |
| BOM | Must not be included |
| Separator | LF (\n); CRLF works because CR is trailing whitespace |
| Blank line | Not a valid value; readers may skip it |
| Final newline | Recommended but not required |
| Value type | Any JSON value, including arrays and scalars |
| MIME type | Not standardized; application/jsonl and application/x-ndjson are common |
The specification names LF as the terminator and accepts CRLF because surrounding whitespace is ignored when parsing a value; a bare CR is not a separator. A leading UTF-8 BOM is prohibited and commonly makes the first line fail to parse; see recognizing a UTF-8 BOM.
The last line may omit its newline, but adding one is recommended: it makes concatenation safe and lets wc -l count every record. Without it, wc -l reports one fewer record.
Edge cases
- Use file iteration or
split("\n"), notstr.splitlines(). Python'ssplitlines()also splits on U+2028 and U+2029, which are legal unescaped inside JSON strings. jqaccepts a stream of whitespace-separated JSON values, so it will happily read pretty-printed JSON. It is a useful transformer but not a JSON Lines validator by itself.- An escaped newline
\ninside a string is data and does not create a boundary. A raw newline at that position is a syntax error. - A trailing CR before LF is whitespace and is discarded by parsers; it does not become part of the value.
- Concatenating two JSON Lines files is safe only if the first ends with a newline.
- A JSON array of objects is a different format: the array is the top-level value, so a line-oriented reader cannot treat each element as a record.
Practical checks
- Validate every non-blank line with a JSON parser and count successes; physical line counts undercount when the final newline is missing.
- Emit records with a compact serializer, such as
jq -cor Python'sjson.dumps(value, separators=(",", ":")), so no value spans lines. - Check the first bytes for a BOM and confirm the file decodes as UTF-8 before streaming.
- Test one file with CRLF endings and one without a final newline.
- When using
jqto inspect records, remember that a missing field becomesnullrather than an error; see jq missing fields and null.
Sources
Revision history
- Published
- Updated
- — Initial publication.