@push-stream-std/push-json-parse (0.0.8)
Installation
@push-stream-std:registry=https://hub.kl1.tenere.ai/api/packages/push-stream-std/npm/npm install @push-stream-std/push-json-parse@0.0.8"@push-stream-std/push-json-parse": "0.0.8"About this package
@push-stream-std/push-json-parse
Streaming JSON parsing.
Published as @push-stream-std/push-json-parse.
Synopsis
import { parse, parseArray, parseLines } from '@push-stream-std/push-json-parse';
import { values } from '@push-stream-std/push-pushable';
import { collect } from '@push-stream-std/push-concat';
// Parse a JSON array arriving in chunks
const chunks = ['[1,2,', '3,"hello"]'];
values(chunks)
.pipe(parse({ mode: 'array' }))
.pipe(collect((err, results) => {
console.log(results); // [[1, 2, 3, 'hello']]
}));
// Parse individual JSON lines (NDJSON)
values(['{"a":1}\n{"b":2}\n'])
.pipe(parse({ mode: 'lines' }))
.pipe(collect((err, results) => {
console.log(results); // [{a: 1}, {b: 2}]
}));
Install
npm config set @push-stream-std:registry https://hub.kl1.tenere.ai/api/packages/push-stream-std/npm/
npm config set -- //hub.kl1.tenere.ai/api/packages/push-stream-std/npm/:_authToken "$PACKAGE_TOKEN"
npm install @push-stream-std/push-json-parse
Why
Parses JSON data streaming through a push-stream, handling partial chunks that may arrive across multiple writes. Supports three modes: 'array' for parsing comma-separated JSON array elements as they arrive (tracking depth and string escaping), 'lines' for newline-delimited JSON, and 'complete' for a single top-level JSON value. Remaining unparseable data at stream end causes the sink's end to be called with the parse error.
API
parse(options)
Creates a through stream that parses incoming string data as JSON.
Parameters:
options.mode(string, default'array') — Parsing mode:'array','lines', or'complete'.
Returns: A push-stream through.
parseArray(options)
Convenience wrapper that calls parse({ ...options, mode: 'array' }).
Returns: A push-stream through.
parseLines(options)
Convenience wrapper that calls parse({ ...options, mode: 'lines' }).
Returns: A push-stream through.
Tests
npm test
Tests cover parsing of complete arrays, partial chunk assembly, newline-delimited JSON lines, single complete values, empty inputs, and malformed JSON error handling.
Requirements
Node.js 22 or later. ESM only.