What Is NDJSON? | NDJSON Viewer Docs

What Is NDJSON?

NDJSON — also called JSON Lines, JSONL, or newline-delimited JSON — is a format specification with exactly two rules. It is published at jsonlines.org:

  1. Each line contains exactly one JSON value: an object, an array, a string, a number, true, false, or null.
  2. Lines are separated by a line feed (\n).

A trailing newline at the end of the file is conventional, so a correctly written file ends with an empty line. Readers ignore it, and a blank line in the middle of a file is ignored too. The recommended media type is application/x-ndjson, registered with IANA. Files are usually named .ndjson or .jsonl; the two extensions are interchangeable, which is why search results mix them freely.

One line is one complete JSON document

Here is an entire file. Three records, three lines:

{"id": 1, "event": "page_view", "path": "/pricing", "ms": 812}
{"id": 2, "event": "signup", "path": "/pricing", "ms": 1940}
{"id": 3, "event": "error", "path": "/checkout", "ms": 233}

Lift any line out on its own and JSON.parse it — it is a complete, valid JSON document. That is the entire contract. The lines are a sequence, not fragments of one larger document, and there is no wrapper between them.

Why the format exists

The rule was designed around a single property: a reader can start consuming before the writer has finished. There is no closing bracket to wait for, no length prefix to seek past, no record count at the front. A consumer following a live file can tail it and process each record the moment its newline lands.

That buys three things a JSON array cannot:

  • Streaming. A producer emits a record immediately instead of buffering a batch and rewriting the whole document.
  • Append-only writes. Adding record number 10,001 is an append. No re-serialisation, no rewriting, no lock between a reader and a writer holding the same handle.
  • Recoverable failure. Because records are independent, a corrupt record invalidates only itself. In one big JSON document, a single bad byte in the middle costs you the entire file.

How it differs from a JSON array

The usual alternative is a document with a wrapping array:

[
  {"id": 1},
  {"id": 2}
]

That is buffered, not streamed. The parser has to reach the final ] before it can tell you there are two records, and the writer must hold or rewrite the whole thing to append one. NDJSON has no such ceiling, because nothing in the format ever has to be rewritten.

Reading it in JavaScript

The correct reader splits on real line breaks and parses each line on its own:

const text = await file.text();
const lines = text.split("\n");
 
const records = [];
lines.forEach((line, index) => {
  const trimmed = line.trim();
  if (trimmed === "") return; // trailing newline, not an error
  try {
    records.push({ line: index + 1, value: JSON.parse(trimmed) });
  } catch (error) {
    records.push({ line: index + 1, value: null, error: error.message });
  }
});

Two details matter. The split is a plain newline split — never also split on the backslash-n escape sequence that appears inside string values, because those two characters are data, not a record boundary. And every line gets its own try block, so one malformed record cannot abort the loop.

Relationship to application/json-seq

NDJSON is a relative of the older IANA-registered application/json-seq, which prefixes each value with the ASCII record separator 0x1E. Both solve the same streaming problem; json-seq is explicit about framing, while NDJSON is explicit about being the smallest thing that works. In practice NDJSON won, because the newline is a character that every log pipeline, every shell tool, and every text editor already handles correctly.

Inspect a real file

Open a .jsonl file at NDJSON Viewer to see every physical line parsed as an independent record. If your file is actually one big JSON array, the JSON Formatter is the right tool instead.