JSONL vs JSON: What JSON Lines Is and When to Use It
If you have ever opened a huge export and found thousands of separate JSON objects, one per line, with no brackets around them, you have met JSON Lines. It is also called JSONL or NDJSON (newline-delimited JSON). It looks like JSON, but a normal JSON parser will reject the whole file.
What JSON Lines looks like
Each line is one complete JSON value, usually an object. Lines are separated by a newline. There is no outer array and no commas between records.
{"id":1,"name":"Ana","tags":["a","b"]}
{"id":2,"name":"Ben","tags":[]}
{"id":3,"name":"Chen","tags":["c"]}
The same data as a regular JSON file would be one array holding three objects, wrapped in [ and ] and separated by commas.
Why people use it
- You can read it one line at a time. A program does not need to load a 5 GB file into memory to find the first record.
- You can append to it. Writing a new record is just adding a line. With a JSON array you would have to rewrite the closing bracket.
- One bad line does not ruin the file. If a write was cut off, only the last line is damaged.
- Tools split it easily. Commands like
head,tailandsplitwork because each record is a line.
That is why log pipelines, data exports and many machine learning datasets use it.
JSON vs JSONL
| JSON array | JSON Lines | |
|---|---|---|
| Whole file valid JSON | Yes | No, only each line is |
| Streaming and appending | Awkward | Easy |
| Pretty printing | Fine | Not allowed, a record must stay on one line |
| Comments or trailing commas | Not allowed | Not allowed |
| File extension | .json | .jsonl or .ndjson |
Common problems
- Unexpected token or extra data error. You gave a JSONL file to a tool that expects one JSON document. Convert it first, or wrap the lines in an array.
- A pretty-printed record. If a record spans several lines, it is no longer valid JSONL. Minify each record onto one line.
- Blank lines. Usually harmless at the end of a file, but some readers fail on them.
- Records with different keys. Allowed, but when you turn them into a table the columns become the union of all keys.
Converting JSONL
To get a spreadsheet, use the JSONL to CSV converter. It flattens nested objects into columns and reports invalid lines instead of stopping at the first one. To get a normal JSON array, the same page can also be used to inspect lines, and you can check the result in the JSON formatter. Everything runs in your browser, so the file is not uploaded. If your data has nesting you are unsure how to flatten, read flatten nested JSON into CSV.