Viewing Large NDJSON Datasets | NDJSON Viewer Docs

Viewing Large NDJSON Datasets

NDJSON is the format you reach for precisely when the data is too big to load at once — a multi-gigabyte bq export, a training shard, a month of logs. The format is stream-friendly, but a viewer that ignores that still falls over, in the browser tab rather than in a log file.

Two things that never work at scale

Parsing the whole file as one document. JSON.parse on a large NDJSON file throws immediately, but the failure people hit is subtler: reading the file into one string and then splitting it means a full copy of the file in memory before a single record exists. A 2 GB export has just cost you 2 GB, plus the decoded string, plus whatever the runtime does next.

Holding every parsed object in React state. The classic mistake. Ten million small objects cost gigabytes of heap on top of the raw text, and every state update re-reconciles a structure no browser can lay out. The tab gets slow, then white, then gone. Cloning state into local storage or a postMessage boundary makes it worse.

The rule that avoids both: keep the raw line, discard the parsed value. JSON.parse per line stays the right primitive — native, fast, and it produces the best error messages available — but the result of that parse is a short-lived value you throw away after extracting a few display fields.

Parse per line, on a worker, in chunks

Three techniques, and all three are load-bearing:

  • A Web Worker for parsing. JSON.parse is synchronous and blocking. A million lines on the main thread is a frozen tab with no spinner. The worker keeps the UI responsive and lets you report progress while it works.
  • Chunked reads. file.slice() in fixed-size blocks means you never materialise the whole file, and you can post progress after every chunk.
  • One try per line. Line independence is the format's guarantee, so a failure is a one-line event. It never ends the chunk.
// worker.js — parse a File in chunks, keep only lightweight metadata.
let offset = 0;
let lineNumber = 0;
let carry = ""; // trailing partial line, carried into the next chunk
const records = [];
 
async function run(file) {
  const CHUNK = 4 * 1024 * 1024;
  const decoder = new TextDecoder("utf-8");
 
  while (offset < file.size) {
    const buffer = await file.slice(offset, offset + CHUNK).arrayBuffer();
    offset += CHUNK;
 
    // stream: true keeps the decoder's state across chunks, so a
    // multi-byte character split across a boundary is not corrupted.
    const text = carry + decoder.decode(buffer, { stream: true });
    const parts = text.split("\n");
    carry = parts.pop(); // incomplete final line, carry it forward
 
    for (const line of parts) {
      lineNumber++;
      const trimmed = line.trim();
      if (trimmed === "") {
        records.push({ line: lineNumber, status: "empty" });
        continue;
      }
      try {
        const value = JSON.parse(trimmed);
        records.push({
          line: lineNumber,
          status: "valid",
          preview: JSON.stringify(value).slice(0, 160),
          keys: value && typeof value === "object" ? Object.keys(value) : [],
          raw: line, // kept so the value can be rebuilt on demand
        });
      } catch (error) {
        records.push({
          line: lineNumber,
          status: "invalid",
          message: String(error.message),
          column: columnOf(String(error.message), line),
          raw: line,
        });
      }
    }
 
    postMessage({ progress: Math.min(1, offset / file.size) });
  }
 
  postMessage({ done: true, records });
}

Materialise a record only when it is opened

The list holds { line, status, preview, raw } — a few hundred bytes per row instead of a full object graph. When you click a row, the pretty-printed view and the collapsible tree are built from JSON.parse(record.raw) at that moment and discarded on close. Ten million rows in the list, exactly one parsed value alive at a time.

Virtualise the rows

Even with cheap metadata, ten million DOM nodes is not a page. A virtualised list mounts only the rows inside the viewport plus a small overscan window, so the element count stays in the dozens no matter how long the file is. Scrolling cost becomes a constant rather than a function of file size.

Compute stats without blocking

Totals — record count, valid, invalid, empty lines, bytes — plus the top-level key hints are accumulated in the worker during the same chunk loop, then posted once at the end. Recomputing them on the main thread after every chunk would be the most expensive thing in the pipeline. Counting key frequencies on the way past costs one property read per record and yields the schema hints for free.

Everything runs locally

No upload, no backend, no database. Drop the file, paste a snippet, or load a built-in sample at NDJSON Viewer.