parseLines
Parses newline-delimited JSON — one JSON value per line. Covers both NDJSON
(application/x-ndjson, .ndjson) and JSON Lines (.jsonl), which are the same format.
Accepts:
- one strict JSON value per line (each line is read like parseStrict)
\nand\r\nline endings- a trailing line break after the last value, or none
- blank lines, skipped by default (see ParseLinesOptions.blankLines)
- an empty text, which holds zero values
Rejects: comments, trailing commas and any other non-strict JSON on a line, a value spread over several lines, and a leading byte order mark (JSON Lines forbids it).
On invalid input: throws a SyntaxError naming the 1-based line number, with the
original parse error as its cause — unless invalidLines/blankLines say to skip it.
Available since v3.1.3
Import
Section titled “Import”Signature
Section titled “Signature”Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
text | string | The newline-delimited JSON text |
options | ParseLinesOptions | How to handle blank and invalid lines — see ParseLinesOptions |
Returns
Section titled “Returns”T[] — The parsed values, in line order, typed as T[]
Examples
Section titled “Examples”Read NDJSON / JSON Lines
Section titled “Read NDJSON / JSON Lines”One JSON value per line; a trailing line break is optional.
Skip a truncated last line
Section titled “Skip a truncated last line”With invalidLines: “skip”, a line cut by a crash is dropped instead of failing the whole parse.
Fail on an invalid line by default
Section titled “Fail on an invalid line by default”The SyntaxError names the line.
Related Types
Section titled “Related Types”ParseLinesOptions
Section titled “ParseLinesOptions”Options for parseLines and safeParseLines.
