Skip to content

stringifySequence

Serializes values as a JSON text sequence (RFC 7464, application/json-seq): each value is written as the ASCII record separator (RS, U+001E), the compact JSON text, then \n — the exact framing the RFC requires of encoders, as jq --seq writes it.

Formatting:

  • default — each value compact, like JSON.stringify(value)
  • 'canonical' — each value as canonical JSON (RFC 8785): sorted keys, reproducible bytes

Indentation isn’t offered, for symmetry with stringifyLines: the trailing \n is what lets a reader detect a truncated top-level number, true, false or null (RFC 7464 §2.4).

Output: 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). With 'canonical', also a RangeError on NaN, ±Infinity or a lone surrogate (see stringify).

Available since v3.1.3

import { stringifySequence } from '@helpers4/json';
// or, from the all-in-one package (same code, one install):
import { stringifySequence } from 'helpers4/json';
stringifySequence(values: readonly unknown[], formatting?: "canonical"): string
ParameterTypeDescription
valuesreadonly unknown[]The values to write, one per record
formatting"canonical"`‘canonical’` for canonical JSON records; compact JSON by default (optional)

string — The JSON text sequence

Each value is framed as RS, JSON text, line feed (RFC 7464).

stringifySequence([{ id: 1 }, 2])
// => '\u001E{"id":1}\n\u001E2\n'

Each record in canonical form.

stringifySequence([{ b: 1, a: 2 }], 'canonical')
// => '\u001E{"a":2,"b":1}\n'

View source on GitHub