JSON vs JSONL (JSON Lines): Differences, When to Use Each, and How to Convert

6 min·Nov 22, 2026
AI Summary

A JSON file holds one root value, usually an array, so a parser must read the whole file. JSONL (JSON Lines, also called NDJSON) puts one complete JSON value on each line, separated by \n, so files can be streamed, appended to and split without parsing everything. Use JSON for configs and API responses; use JSONL for logs, data exports, mongoimport/BigQuery loads, and LLM batch or fine-tuning datasets. Convert with jq -c ".[]" (JSON → JSONL) and jq -s "." (JSONL → JSON).

Sample JSON Data (Employees, Students, Products)
Ready-to-copy sample JSON data for testing: employees with departments, students with grades, and products with nested variants. Covers every JSON type: strings, numbers, booleans, null, nested objects, arrays and ISO dates.
{
  "departments": [
    {
      "id": "ENG",
      "name": "Engineering",
      "location": "Bengaluru"
    },
    {
      "id": "SAL",
      "name": "Sales",
      "location": "London"
    },
    {
      "id": "HR",
      "name": "People",
      "location": "New York"
    }
  ],
  "employees": [
    {
      "id": 1001,
      "name": "Asha Rao",
      "email": "[email protected]",
      "department": "ENG",
      "title": "Engineering Manager",
      "salary": 145000,
      "currency": "USD",
      "isActive": true,
      "hiredAt": "2019-04-01",
      "manager": null,
      "skills": [
        "go",
        "kubernetes",
        "hiring"
      ],
      "address": {
        "city": "Bengaluru",
        "country": "IN",
        "postalCode": "560001"
      }
    },
    {
      "id": 1002,
      "name": "Daniel Okafor",
      "email": "[email protected]",
      "department": "ENG",
      "title": "Senior Software Engineer",
      "salary": 128000,
      "currency": "USD",
      "isActive": true,
      "hiredAt": "2021-09-13",
      "manager": 1001,
      "skills": [
        "typescript",
        "react",
        "postgres"
      ],
      "address": {
        "city": "Lagos",
        "country": "NG",
        "postalCode": "100001"
      }
    },
    {
      "id": 1003,
      "name": "Mia Fischer",
      "email": "[email protected]",
      "department": "SAL",
      "title": "Account Executive",
      "salary": 92000,
      "currency": "USD",
      "isActive": false,
      "hiredAt": "2022-01-10",
      "manager": null,
      "skills": [
        "negotiation",
        "crm"
      ],
      "address": {
        "city": "London",
        "country": "GB",
        "postalCode": "EC1A 1BB"
      }
    }
  ],
  "students": [
    {
      "id": "S-001",
      "name": "Liam Chen",
      "age": 20,
      "enrolledAt": "2024-08-26T09:00:00Z",
      "course": "Computer Science",
      "grades": {
        "math": 91,
        "physics": 84,
        "programming": 97
      },
      "scholarship": true
    },
    {
      "id": "S-002",
      "name": "Priya Nair",
      "age": 19,
      "enrolledAt": "2025-08-25T09:00:00Z",
      "course": "Economics",
      "grades": {
        "math": 78,
        "statistics": 88,
        "writing": 92
      },
      "scholarship": false
    }
  ],
  "products": [
    {
      "sku": "TSHIRT-BASIC",
      "name": "Basic T-Shirt",
      "price": 19.99,
      "inStock": true,
      "tags": [
        "apparel",
        "cotton"
      ],
      "variants": [
        {
          "size": "M",
          "color": "black",
          "stock": 42
        },
        {
          "size": "L",
          "color": "white",
          "stock": 0
        }
      ]
    },
    {
      "sku": "MUG-12OZ",
      "name": "Ceramic Mug 12oz",
      "price": 9.5,
      "inStock": true,
      "tags": [
        "kitchen"
      ],
      "variants": []
    }
  ]
}

Both formats use exactly the same JSON syntax. The only difference is how many values a file holds.

The difference in one example

JSON: one root value, usually an array.

[ { "id": 1, "event": "login", "user": "asha" }, { "id": 2, "event": "purchase", "user": "daniel", "amount": 5998 } ]

JSONL: one complete value per line, no enclosing array, no commas between records.

{"id": 1, "event": "login", "user": "asha"} {"id": 2, "event": "purchase", "user": "daniel", "amount": 5998}

Comparison

JSONJSONL
Root values per fileExactly oneOne per line
Read without loading everythingHard (needs a streaming parser)Easy: read line by line
Append a recordRewrite the closing ]Write one more line
Split / parallelizeMust parse firstSplit on newlines (split -l)
A corrupt record breaksThe whole fileOnly that line
Pretty-printingAllowedNot allowed (one line per record)
Human readabilityGoodOK for small records
Typical extension.json.jsonl, .ndjson

When to use JSON

  • Config files (package.json, tsconfig.json, settings files): one document, edited by people.
  • API request and response bodies: one value per HTTP message.
  • Small datasets you want to read in an editor.

When to use JSONL

  • Logs and event streams: every log line is a record you can grep, tail -f and append to.
  • Large exports and imports: MongoDB's mongoimport and BigQuery's newline-delimited JSON loads read one record per line.
  • LLM batch and fine-tuning files: OpenAI and Anthropic batch inputs and most fine-tuning datasets are JSONL, one request or example per line.
  • Data pipelines: Spark, DuckDB and pandas (lines=True) all read JSONL directly and can process it in chunks.

A rule of thumb: if the data is a document, use JSON. If it is a stream of records, use JSONL.

Converting with jq

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

-c prints each value compactly on one line. -s (slurp) collects all lines into one array.

Converting with Python

import json # JSON → JSONL with open("employees.json") as src, open("employees.jsonl", "w") as dst: for row in json.load(src): dst.write(json.dumps(row, ensure_ascii=False) + "\n") # Stream a JSONL file without loading it all with open("events.jsonl") as f: for line in f: if line.strip(): event = json.loads(line)

With pandas: pd.read_json("events.jsonl", lines=True) and df.to_json("out.jsonl", orient="records", lines=True).

Common JSONL mistakes

  • Pretty-printing records. A record spread over several lines breaks every JSONL reader.
  • Wrapping the file in [ ] or adding commas between lines. That makes it neither valid JSON nor valid JSONL.
  • Windows line endings. Most readers accept \r\n, but some strict tools do not. Write \n.
  • A blank last line. Usually fine, but skip empty lines when you parse.

Related comparisons

FAQ

Is JSONL the same as NDJSON?

In practice, yes. Both mean newline-delimited JSON: one valid JSON value per line, UTF-8, separated by \n. The file extensions are .jsonl and .ndjson.

Is a JSONL file valid JSON?

No. A JSONL file with more than one line has several root values, which a normal JSON parser rejects. Each individual line is valid JSON.

Can a JSONL line be pretty-printed over several lines?

No. Each record must be on a single line, with no raw newlines inside it. Newlines inside strings are escaped as \n, which JSON serializers do automatically.

Why do LLM APIs use JSONL for batch and fine-tuning files?

Each line is one independent request or training example. The service can stream the file, process lines in parallel and report errors per line, which a single huge JSON array would not allow.