Skip to content

isNdjson

Checks whether a value is a string holding valid newline-delimited JSON — one strict JSON value per line, as NDJSON and JSON Lines (.jsonl) both define it.

It accepts exactly what @helpers4/json’s parseLines accepts with its default options: blank lines are tolerated (NDJSON allows them; JSON Lines doesn’t), and an empty string is valid — it holds zero values.

Available since v3.1.3

import { isNdjson } from '@helpers4/guard';
// or, from the all-in-one package (same code, one install):
import { isNdjson } from 'helpers4/guard';
isNdjson(value: unknown): value is string
ParameterTypeDescription
valueunknownThe value to check

value is string — true if value is a string that parseLines from @helpers4/json reads without error

One strict JSON value per line.

isNdjson('{"id":1}\n{"id":2}\n') // => true

Pretty-printed JSON is not NDJSON.

isNdjson('{\n  "id": 1\n}') // => false

View source on GitHub