JSON vs JSONL: Understanding JSON Lines Format

Learn the differences between JSON and JSONL and when to use each format

Published: January 2025 • 8 min read

If you work with JSON data, you might have come across JSONL (also called JSON Lines or NDJSON). While they look similar, they're actually quite different in how they structure data. Understanding the difference between JSON and JSONL can save you a lot of headaches when processing large datasets or streaming data.

In this guide, we'll break down what makes JSONL different from regular JSON, when you should use each format, and how they handle different use cases. Whether you're working with log files, streaming data, or just curious about different JSON formats, this article will help you understand which one fits your needs.

Need to work with JSON and JSONL? Try our JSON formatter,JSON validator,JSON to JSONL, orJSONL to JSON converter tools.

What is JSONL (JSON Lines)?

JSONL (JSON Lines) is a format where each line is a valid JSON object, and lines are separated by newline characters. Think of it as a text file where every single line contains one complete JSON object. It's also known as NDJSON (Newline Delimited JSON) or JSON streaming format.

The beauty of JSONL is its simplicity: you can process one line at a time without loading the entire file into memory. This makes it perfect for handling huge datasets, log files, or streaming data where you don't want to wait for everything to load before you start processing.

Key point: In JSONL, the top-level structure cannot be an array or object that spans multiple lines. Each line must be a complete, self-contained JSON value (usually an object).

Side-by-Side Comparison

Let's see the same data represented in both formats. Here's a list of user records:

JSON Format

Array of Objects
[
  {
    "name": "Alice",
    "age": 30,
    "city": "New York"
  },
  {
    "name": "Bob",
    "age": 25,
    "city": "London"
  },
  {
    "name": "Charlie",
    "age": 35,
    "city": "Tokyo"
  }
]
Structure: One JSON array containing all objects
Parse: Must load entire file into memory

JSONL Format

Line-by-Line
{"name":"Alice","age":30,"city":"New York"}
{"name":"Bob","age":25,"city":"London"}
{"name":"Charlie","age":35,"city":"Tokyo"}
Structure: Each line is a separate JSON object
Parse: Process one line at a time

Notice the difference? Regular JSON wraps everything in square brackets [], while JSONL just puts each object on its own line. No commas between objects, no wrapping array – just newlines.

Key Differences

1. Structure

JSON:

Single document with nested structures. Everything is wrapped in one root element (usually an array or object).

JSONL:

Multiple independent documents, one per line. Each line is a complete JSON value that can be parsed independently.

2. Parsing

JSON:

You typically need to load and parse the entire file at once. Great for small files, challenging for large ones.

JSONL:

You can read and parse line by line. Perfect for streaming or when memory is limited.

3. Appending Data

JSON:

To add new data, you need to rewrite the entire file (close the array, add comma, add item, reopen array).

JSONL:

Just append a new line to the end of the file. No need to modify existing content. Perfect for logs!

4. Error Recovery

JSON:

If there's an error anywhere in the file, the entire document might be invalid and unparseable.

JSONL:

A corrupted line only affects that one record. You can skip the bad line and continue processing the rest.

Complete Comparison Table

FeatureJSONJSONL
StructureSingle documentOne object per line
Memory UsageLoads entire fileStreams line by line
Large FilesProblematicExcellent
Appending DataMust rewrite fileJust add a line
Human ReadabilityVery readableLess readable (compact)
Streaming SupportLimitedNative
Error RecoveryEntire file failsPer-line recovery
Parallel ProcessingDifficultEasy (split by lines)
Browser SupportNativeRequires custom parsing
Nested StructuresFull supportWithin each line
Best Use CaseAPIs, config filesLogs, streaming, big data

When to Use Each Format

Use JSON When:

  • Building web APIs: JSON is the standard for REST APIs and works natively with JavaScript/browsers
  • Configuration files: JSON is easy to read and edit by humans when properly formatted
  • Small to medium datasets: If your data fits comfortably in memory, JSON is perfect
  • Complex nested structures: When you need deep nesting and want it to be readable
  • Browser applications: Native JSON.parse() and JSON.stringify() make it incredibly easy

Use JSONL When:

  • Processing log files: Each log entry can be a separate line, easy to append and process
  • Streaming data: Process data as it arrives without waiting for everything to load
  • Large datasets: When files are too big to load into memory all at once
  • ETL pipelines: Extract, transform, load operations benefit from line-by-line processing
  • Machine learning datasets: Training data often comes in JSONL format for efficient batch processing
  • Parallel processing: You can easily split a JSONL file by lines and process chunks in parallel

Working with JSONL in Code

Here's how you can read and write JSONL files in different programming languages:

Python Example

import json

# Reading JSONL file
with open('data.jsonl', 'r') as f:
    for line in f:
        obj = json.loads(line)
        print(obj)

# Writing JSONL file
data = [
    {"name": "Alice", "age": 30},
    {"name": "Bob", "age": 25}
]

with open('output.jsonl', 'w') as f:
    for obj in data:
        f.write(json.dumps(obj) + '\n')

Node.js Example

const fs = require('fs');
const readline = require('readline');

// Reading JSONL file
const rl = readline.createInterface({
  input: fs.createReadStream('data.jsonl')
});

rl.on('line', (line) => {
  const obj = JSON.parse(line);
  console.log(obj);
});

// Writing JSONL file
const data = [
  {name: 'Alice', age: 30},
  {name: 'Bob', age: 25}
];

const output = data.map(obj => JSON.stringify(obj)).join('\n');
fs.writeFileSync('output.jsonl', output);

Real-World Use Cases

Application Logs

Every log entry is a JSONL line. Easy to append new logs, search through them, and process with standard Unix tools like grep, tail, etc.

cat app.log | grep "error"

Data Exports

Database exports often use JSONL because you can stream millions of records without loading everything into memory.

pg_dump | jsonl-processor

Machine Learning

Training datasets in JSONL format allow models to load data in batches, essential for large datasets that don't fit in RAM.

Event Streaming

Real-time event systems often use JSONL to stream events one at a time, allowing consumers to process data as it arrives.

Converting Between JSON and JSONL

Sometimes you need to convert between the two formats. Here's how:

JSON Array → JSONL

# Python
import json

with open('data.json', 'r') as f:
    array = json.load(f)

with open('data.jsonl', 'w') as f:
    for item in array:
        f.write(json.dumps(item) + '\n')

JSONL → JSON Array

# Python
import json

array = []
with open('data.jsonl', 'r') as f:
    for line in f:
        array.append(json.loads(line))

with open('data.json', 'w') as f:
    json.dump(array, f, indent=2)

Helpful JSON Tools

Final Thoughts

JSON and JSONL aren't competing formats – they're tools for different jobs. Regular JSON is great when you need a structured document that's easy to read and work with in most programming environments. JSONL shines when you're dealing with large volumes of data, streaming scenarios, or situations where you need to process records independently.

Think of JSON as a book – everything is organized together in one complete structure. JSONL is more like a stack of index cards – each card is independent, and you can add, remove, or process them one at a time without touching the others.

Quick decision guide:

Use JSON for APIs, config files, and when human readability matters. Use JSONL for logs, data pipelines, streaming, and any time you're processing lots of independent records. When in doubt, JSON is the safer choice for most applications.

Related Articles