JSON Lines (NDJSON) Explained
Understand the JSON Lines format, its advantages, and when to use it instead of traditional JSON.
JSON Lines, also known as NDJSON (Newline Delimited JSON), is a file format where each line contains one complete JSON object. Instead of storing an entire dataset inside a single JSON array, every record is written independently on its own line.
This simple approach makes processing massive datasets significantly easier because applications can read one record at a time instead of loading an entire file into memory.
What Is NDJSON?
NDJSON stands for Newline Delimited JSON. The format consists of multiple valid JSON objects separated by newline characters. Each line is independent and can be parsed individually.
{"id":1,"name":"Alice"}
{"id":2,"name":"Bob"}
{"id":3,"name":"Charlie"}Notice that there is no surrounding array and no commas between objects. Every line is a complete JSON document by itself.
Regular JSON vs NDJSON
Although both formats store JSON data, they are designed for different purposes.
| Regular JSON | NDJSON |
|---|---|
| Usually one JSON document | Many JSON documents |
| Objects stored inside arrays | One object per line |
| Entire file parsed together | Lines parsed independently |
| Good for APIs | Excellent for streams and logs |
| Requires complete document | Can be processed continuously |
Traditional JSON Example
[
{
"id": 1,
"name": "Alice"
},
{
"id": 2,
"name": "Bob"
}
]This is the standard JSON format most developers are familiar with. Every object belongs to a single array, meaning the parser generally expects the complete document before processing.
The Same Data as NDJSON
{"id":1,"name":"Alice"}
{"id":2,"name":"Bob"}Here each line represents an independent JSON object. New records can simply be appended to the end of the file without modifying existing data.
Why NDJSON Exists
Loading a multi-gigabyte JSON file into memory is often slow or even impossible. NDJSON solves this problem by allowing applications to process one object at a time.
Instead of waiting until the complete document has been received, software can immediately parse each incoming line and continue processing while additional data is still arriving.
Common Use Cases
- Application log files.
- Streaming APIs.
- Big data pipelines.
- Machine learning datasets.
- Data import and export tools.
- Event processing systems.
Streaming Data
Streaming is one of NDJSON's greatest strengths. Because each record is complete on its own, producers can continuously send new lines while consumers process them immediately.
This makes NDJSON particularly useful for real-time analytics, monitoring dashboards and systems that generate continuous event streams.
Memory Efficiency
One of the biggest advantages of NDJSON is low memory usage. Rather than loading an entire dataset, applications typically read a single line, process it and discard it before moving to the next record.
Read line
ā
Parse JSON
ā
Process object
ā
Read next lineThis approach scales well even when working with files containing millions of records.
Appending New Records
Unlike traditional JSON arrays, NDJSON files can easily grow over time. Adding a new record simply means writing another line to the end of the file.
No commas need to be inserted, and there is no closing bracket to update, making NDJSON ideal for continuously generated data such as logs.
Validating NDJSON
Each line must be valid JSON on its own. An invalid object affects only that line rather than the entire file, although many processing tools will stop when encountering malformed input.
{"id":1,"name":"Alice"}
{"id":2,"name":"Bob"}
{"id":3,"name":"Charlie"}Using a JSON Lines Validator helps detect malformed records before importing or processing the data.
Formatting NDJSON
Because each line must contain exactly one complete JSON object, pretty-printing an entire file across multiple lines would break the NDJSON format.
Specialized JSON Lines Formatter tools preserve the one-record-per-line structure while normalizing whitespace inside each object.
Converting Between JSON and NDJSON
Converting from JSON to NDJSON is straightforward when the JSON document contains an array. Each array element becomes one line in the output file.
[
{ "id": 1 },
{ "id": 2 },
{ "id": 3 }
]becomes
{"id":1}
{"id":2}
{"id":3}Similarly, an NDJSON to JSON Converter combines individual records into a standard JSON array.
When Should You Use NDJSON?
- Processing very large datasets.
- Streaming API responses.
- Log collection systems.
- Analytics pipelines.
- Machine learning datasets.
- Applications that append records continuously.
When Regular JSON Is Better
For most REST APIs, configuration files and frontend applications, traditional JSON remains the preferred choice because it is universally supported and represents a complete document.
If your data naturally forms a single object or array and isn't processed incrementally, standard JSON is usually simpler.
Common Mistakes
- Wrapping NDJSON records inside an array.
- Separating records with commas.
- Pretty-printing objects across multiple lines.
- Mixing plain text with JSON records.
- Assuming every JSON parser supports NDJSON automatically.
Frequently Asked Questions
Is NDJSON valid JSON?
No. An NDJSON file is a sequence of individual JSON documents rather than one complete JSON document. Each line is valid JSON, but the entire file is not valid standard JSON.
Why is NDJSON faster for large files?
Applications can process one record at a time instead of loading the entire dataset into memory, reducing memory usage and enabling streaming.
Can I pretty-print NDJSON?
Not across multiple lines. Each JSON object must remain on a single line to preserve the NDJSON format.
Can I convert NDJSON to JSON?
Yes. Each line becomes an element in a JSON array, and many tools can perform this conversion automatically.
What is NDJSON mainly used for?
NDJSON is commonly used for log files, streaming APIs, analytics pipelines, event processing and other systems that handle large or continuously growing datasets.
Conclusion
JSON Lines (NDJSON) is a lightweight format designed for streaming and processing large collections of JSON records efficiently. By storing one JSON object per line, it enables incremental reading, low memory usage and simple appending of new data. Whether you're working with logs, analytics or data pipelines, understanding NDJSON and using tools such as an NDJSON Viewer, JSON Lines Validator, Formatter and NDJSON to JSON Converter can make working with large datasets much more efficient.