NDJSON in practice: split, fix and ship logs

25 NOVEMBER 2026 · 8 MIN READ

The format argument is short; the working life of a JSON Lines file is where the time goes. A file that will not convert, a record that must be dropped, a sample that needs a schema and a bulk import that wants pairs — each has a small, repeatable fix.

The format comparison covers why one record per line is worth it. This page is the other half: what actually goes wrong in a real file, and the shortest path through it. Everything here assumes the one property that makes the format useful — a record never spans a line — which is also the property most often broken.

The conversion that fails first

Paste a pretty-printed array of objects into NDJSON to JSON and the first line is { on its own: an incomplete value. The reader reports the line rather than swallowing it, which is the whole point of the format — one bad record must cost you one record, not the file.

What a broken line reports
input                          output
{"level":"info"}              Line 2 is not valid JSON: nope (Unexpected token 'o', "nope" is not valid JSON)
nope
{"level":"warn"}

{                              Line 1 is not valid JSON: { (Expected property name or '}' in JSON at position 1)
  "a": 1
}
The convert module reading three NDJSON lines with a broken record in the middle and reporting that line 2 is not valid JSON, quoting the offending record
One bad record costs one record, and the message names it

Armed with a line number, the repair is mechanical. Delete the record and keep going, or keep it and fix it. In a shell that is sed -i '2d' app.ndjson; on a read-only file, grep -v the offender out or split the stream at the boundary:

Split around one bad record
head -n 1 app.ndjson  > good.ndjson
tail -n +3 app.ndjson >> good.ndjson
awk 'NF' app.ndjson > clean.ndjson   # drop blank lines

Recipes worth memorising

  1. 01 Take a sample. head -n 500 app.ndjson > sample.ndjson. Converting a sample instead of the whole file is the difference between a two-second answer and a browser tab that never comes back.
  2. 02 Filter without a converter. No decoding step is needed for a line stream: jq -c 'select(.level == "error")' app.ndjson emits NDJSON again, because jq reads and writes a stream of values by default.
  3. 03 Get the whole set in one value. Convert the file to an array, and every tool that wants a root document works: JSONPath over the collection, schema inference across all records, CSV for a spreadsheet, diff between two exports.
  4. 04 Count records honestly. wc -l equals the record count only if every writer escaped the newlines inside its messages. One raw newline turns one record into two lines and the count into a lie; the check is that the number of lines equals the number of records the converter produced.
  5. 05 Ship to a bulk API. Elasticsearch and OpenSearch want an action line before each document line — two lines per record, neither pretty-printed, and the payload has to end with a newline.
  6. 06 Rotate and concatenate. Compressed segments glue together: zcat part-*.gz > all.ndjson works because each member is an independent byte stream that ends on a line boundary.
Bulk import shape
{"create":{"_index":"events"}}
{"ts":"2026-11-25T08:00:01Z","level":"info","msg":"boot"}
{"create":{"_index":"events"}}
{"ts":"2026-11-25T08:00:04Z","level":"warn","msg":"slow query"}

What actually breaks these files

SymptomCauseFix
Line 1 fails on {a pretty-printed array pasted as if it were linesconvert the array with JSON to NDJSON instead of minifying it
The last line is a fragmentthe writer was interrupted mid-recorddrop the trailing partial line; a truncated record is unrecoverable, the rest is fine
A blank line is reportedsome writers emit one, most readers skip themharmless here — blank lines are treated as framing, not records
Only the first line failsa UTF-8 BOM at offset 0strip the BOM; every later record was fine
One record contains a raw newlinea message field serialised by hand rather than by a writerescape it as \n at the producer; no reader can recover a line boundary that was never written
Two records on one linea writer that used a comma instead of a newlinesplit on the boundary between } and {, then reconvert
Every line starts with a timestampa log prefix before the JSON bodycut the prefix off per line — sed 's/^.*\] //' — and then treat the remainder as records
Numbers in ids changed shapeeach line parses as JSON, so doubles applyidentifiers must be quoted by the writer; see the precision guide

Both directions

Array to lines writes one compact record per line and terminates the file with a newline, which is what most importers expect. It accepts a non-array root and writes it as a single line, and it handles scalar arrays, so [1, "x", null] becomes three lines — legal NDJSON even though it looks odd. Going back, lines to an array gives you one document out of many, and the conversion is not symmetric: a pretty-printed input is not “the same file with different spacing”, it is a different number of records. When in doubt, convert to an array, inspect it in tree form, and convert out again — that round trip shows you whether the line boundaries were where you thought they were.

NOTE If the file is not JSON at all but a key=value log, NDJSON is still the right destination: parse into records with a script, emit one object per line, and the whole toolchain above — sampling, filtering, schema inference, CSV — becomes available without writing a parser for each job.

TOOL MATRIX

Tools for this guide

Keyboard shortcuts
FORMATTING
Format input Ctrl/⌘ + Enter
Toggle auto-format Ctrl/⌘ + Shift + A
CLIPBOARD & FILES
Copy output Ctrl/⌘ + K
Open JSON file Ctrl/⌘ + O
Download output Ctrl/⌘ + S
Save snippet Ctrl/⌘ + Shift + N
MODULES
Open find & replace Ctrl/⌘ + Shift + F
Lock / unlock output Ctrl/⌘ + Shift + L
Show this dialog Ctrl/⌘ + /
Local history
Save snippet
Erase local data

Nothing is stored in this browser yet.

Removes the saved draft, history, snippets and formatting options for this site. Documents you typed stay in the editor until the page reloads.