DataSwitch Pro Private, in-browser data converter

JSON Lines (JSONL) Explained: Format, Uses and Common Errors

By the DataSwitch Pro team · · 4 min read

JSON Lines — also called JSONL, or NDJSON for newline-delimited JSON — is a simple variation of JSON: instead of one big document, a file contains one complete JSON value per line. That small change makes it one of the most practical formats for large datasets, logs and machine-learning training data.

The format in one example

events.jsonl

{"ts": "2026-11-10T08:00:01Z", "level": "info", "msg": "server started"}
{"ts": "2026-11-10T08:00:07Z", "level": "warn", "msg": "slow query", "ms": 812}
{"ts": "2026-11-10T08:01:12Z", "level": "error", "msg": "db timeout"}

The rules are short: the file is UTF-8 text, each line is a valid JSON value (almost always an object), and lines are separated by a newline character. There are no commas between records and no surrounding square brackets. The same data as a regular JSON array would be wrapped in [ … ] with commas between the objects.

Why not just use a JSON array?

The trade-off is readability: records cannot be pretty-printed across several lines, so a JSONL file is harder for humans to scan. Converting it to a formatted array with the JSONL to JSON converter makes inspection easy.

Where JSONL is used

JSONL for fine-tuning datasets

For chat-model fine-tuning, a common structure is one object per line containing a messages array of role/content pairs:

train.jsonl

{"messages": [{"role": "system", "content": "You answer in one short sentence."}, {"role": "user", "content": "What is the capital of Morocco?"}, {"role": "assistant", "content": "Rabat is the capital of Morocco."}]}
{"messages": [{"role": "system", "content": "You answer in one short sentence."}, {"role": "user", "content": "Translate 'thank you' into French."}, {"role": "assistant", "content": "Merci."}]}

Each provider documents its own required fields, so always check the current specification before uploading. A practical workflow is to build and edit your examples as a regular, pretty-printed JSON array — or even in a spreadsheet — and convert to JSONL at the end with the JSON to JSONL converter.

The most common JSONL errors

The JSONL to JSON converter parses the file line by line and reports the exact line number of the first invalid record, which is the quickest way to find these mistakes in a large file.

Reading and writing JSONL in code

Python

import json

# Read
with open("data.jsonl", encoding="utf-8") as f:
    records = [json.loads(line) for line in f if line.strip()]

# Write
with open("out.jsonl", "w", encoding="utf-8") as f:
    for r in records:
        f.write(json.dumps(r, ensure_ascii=False) + "\n")

Command line (jq)

jq -c '.[]' data.json > data.jsonl      # JSON array -> JSONL
jq -s '.' data.jsonl > data.json        # JSONL -> JSON array

Summary

JSON Lines is JSON with one record per line. It is ideal for logs, large datasets and AI training files because it can be streamed, appended to and split easily. Keep every record on a single line, escape line breaks inside strings, use UTF-8, and validate the file line by line before uploading it anywhere.

Try it on your own file. Free, private, and processed in your browser.

Frequently asked questions

What file extension should I use?

.jsonl is the most common; .ndjson is also widely recognised. Both describe the same format.

Can a JSONL line contain an array instead of an object?

Yes, any JSON value is valid on a line, but objects are the norm, and most tools that consume JSONL expect them.

Is an empty line allowed?

Most readers skip blank lines, and this site's converter does too, but strict tools may reject them. It is safest to avoid them, except for a single trailing newline at the end of the file.

More guides