How to Parse JSON Lines and Find Invalid Records
Parse NDJSON line by line, diagnose malformed records, and preserve useful error locations during imports.

An NDJSON import can fail because just one record is malformed. Parsing the whole file as JSON obscures the underlying issue because multiple adjacent objects are not one valid JSON document. Split records at physical line boundaries and parse each completed record independently.
Keep line numbers during validation
Count physical lines before discarding blank lines so error messages point to the source file accurately. Our converter ignores blank lines and reports the original line number for malformed JSON. A stricter ingestion system may reject blank lines, so confirm its contract before exporting. A final newline is helpful for text processing and is commonly expected in NDJSON files.
Recognize common broken records
Trailing commas, single-quoted property names, and unescaped control characters are invalid JSON. A pretty-printed object spanning multiple lines is valid standalone JSON but invalid as one NDJSON record. A string containing a line break must use an escaped newline. If records arrive over a network stream, buffer incomplete chunks until a newline arrives: network chunks do not necessarily align with records.
Choose a failure policy explicitly
For a production import, decide whether to stop at the first invalid line or put bad records in a separate error file. Silently dropping records changes counts and can hide data loss. Record the line number and diagnostic while avoiding sensitive payloads in logs. Validate the resulting array before writing to a database, because valid JSON syntax alone does not establish required fields, types, or business rules.
Worked example
const rows = text.split(/
?
/);
const values = rows.filter(line => line.trim()).map(line => JSON.parse(line));Why does JSON.parse reject an NDJSON file?
Multiple JSON values separated by newlines are not a single JSON document. Parse individual lines or convert them to an array first.


