stringifyLines
Serializes values as newline-delimited JSON — one compact JSON value per line, each followed
by \n. The output is both NDJSON and JSON Lines (.jsonl), which are the same format.
Formatting:
- default — each value compact, like
JSON.stringify(value) 'canonical'— each value as canonical JSON (RFC 8785): sorted keys, reproducible bytes, for hashing or diffing logs line by line
Indentation isn’t available: a line break inside a value would split it across lines. String
values can’t break a line either — JSON.stringify escapes \n and \r inside them.
Output: every value is followed by \n, including the last one (NDJSON requires it, and
JSON Lines recommends it, so files can be concatenated). An empty array gives ''.
Throws a TypeError when values isn’t an array, or when one of them has no JSON
representation (undefined, a function, a symbol, a BigInt, a circular structure) — rather
than writing a line that would read back as something else. With 'canonical', also a
RangeError on NaN, ±Infinity or a lone surrogate (see stringify).
Available since v3.1.3
Import
Section titled “Import”Signature
Section titled “Signature”Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
values | readonly unknown[] | The values to write, one per line |
formatting | "canonical" | `‘canonical’` for canonical JSON lines; compact JSON by default (optional) |
Returns
Section titled “Returns”string — The newline-delimited JSON text
Examples
Section titled “Examples”Write NDJSON / JSON Lines
Section titled “Write NDJSON / JSON Lines”One compact value per line, each followed by a line break.
Canonical lines
Section titled “Canonical lines”Each line in canonical form, so a log can be hashed or diffed line by line.
