Fixing Malformed JSONL Records | NDJSON Viewer Docs

Fixing Malformed JSONL Records

The encouraging thing about newline-delimited JSON is that a broken line almost never means a broken file. Line independence is the format's core guarantee, so a malformed record invalidates only itself. Your job is to find it, read it, and fix it — not to rebuild the export.

The real causes of one bad line

A literal newline inside a string. This is the most common cause by a wide margin, and it is the easiest to misdiagnose. A JSON string may not contain a raw line break; it must contain the two-character escape sequence \n. A writer that interpolated a multi-line stack trace emitted a real newline, and one record is now two physical lines:

{"id": 42, "error": "connection reset
by peer"}

Both halves fail to parse, and they are usually reported as two separate invalid records that are really one.

Interleaved or truncated concurrent appends. Two threads writing the same descriptor without synchronisation interleave their bytes mid-record, and a process killed mid-write leaves a record with no closing brace. The symptom is a line that starts with { and stops mid-object.

Trailing commas. A hand-edited line, or a writer that appends a separator after the last member, produces {"a":1,} — valid JavaScript object syntax, invalid JSON.

Single quotes and unquoted keys. {'id': 1} and {id: 1} are JavaScript object syntax, not JSON. JSON requires double-quoted keys and string values.

A UTF-8 byte order mark on line one. Windows exporters frequently prepend the bytes EF BB BF. They decode as an invisible character glued to the first key, so the first record fails while every other record is fine.

Mixed CR-only line endings. Old Mac line endings produce one enormous "line" containing the whole file, which then fails as a single record.

A naive split. A reader that also splits on the escape sequence inside string values cuts records in half and fakes the illusion of a literal newline. If the reader is the culprit, fixing the writer will not help.

A missing final newline. A file whose last record is not newline-terminated is still valid and still fully parseable. It is only a problem if your reader assumes the terminator and drops the tail — a bug that looks like a truncated record.

Before and after

A pipeline wrote a record with an embedded stack trace. Four physical lines where there should have been one:

{"id": 41, "status": "ok"}
{"id": 42, "error": "connection reset
    at TCPConnectWrap.afterConnect
    at listOnTimeout"}
{"id": 43, "status": 'ok'}
{"id": 44, "status": "ok",}

Every line after {"id": 41, ...} is invalid. Fix the stack trace by escaping it and normalise the other two records, and you get a file where every line parses:

{"id": 41, "status": "ok"}
{"id": 42, "error": "connection reset\n    at TCPConnectWrap.afterConnect\n    at listOnTimeout"}
{"id": 43, "status": "ok"}
{"id": 44, "status": "ok"}

Three separate changes: the newline inside error became an escape sequence, the single quotes became double quotes, and the trailing comma went away.

The triage workflow

1. Count first. The totals for records, valid, and invalid tell you the scale. One invalid line in 400,000 is a typo; four thousand is a broken exporter, and the fix belongs upstream.

2. Filter to the invalid records only. Switch the filter from All to Invalid and the list collapses to the casualties. Read them in line order — the earliest failure is usually the cause of the rest.

3. Jump to the reported column. The humanised error names the character position where parsing stopped, and the position tells you the shape of the problem before you read a character: at the end of the line means a truncation or unclosed brace, at the start means a BOM, just past a comma means a trailing comma, on a key name means unquoted keys.

4. Inspect the raw line. The raw-line inspector shows the original text next to the parsed view. This is where a literal newline is unmistakable — the record visibly runs past the end of its own row.

5. Fix and re-run. Apply the fix where the record was produced, not in the export, or it returns on the next run. Re-open the file and confirm the invalid count reached zero.

Skip the broken ones in the meantime

While you track down the writer, export the valid records. The export is written one record per line, so jq, bq load, and pandas.read_json(lines=True) consume it directly. The invalid set is exported separately, so nothing is actually lost.

Load the file at NDJSON Viewer to get flagged records, reported columns, and the raw-line inspector. Parsing runs locally in your browser.