@simple-api-stream/json
Streaming JSON (SAJ = Simple API for JSON) parser and serializer built on the Web Streams API.
Installation
npm install @simple-api-stream/json
Requires Node.js >= 24 or any runtime supporting Web Streams and async iteration of ReadableStream.
Features
| Export |
Type |
Input |
Output |
JSONTextToSAJTransformStream |
TransformStream |
JSON text (string chunks) |
SAJEventInterface objects |
JSONTextToSAJEventWritableStream |
WritableStream |
JSON text (string chunks) |
Calls a SAJHandler |
SAJToObjectTransformStream<T> |
TransformStream |
SAJ events |
JavaScript values of type T |
ObjectToSAJTransformStream<T> |
TransformStream |
JavaScript values (incl. toJSON(), iterables, async iterables) |
SAJ events |
SAJToJSONTextTransformStream |
TransformStream |
SAJ events |
JSON text |
ResolveToSAJReadableStream |
ReadableStream |
Method calls (SAJResolver) |
SAJ events |
parseJSONResponse<T> |
Helper function |
Fetch Response |
ReadableStream<T> (auto-detects NDJSON / JSONL / json-seq) |
Usage
import { parseJSONResponse } from "@simple-api-stream/json";
const response = await fetch("https://example.com/data.json");
for await (const obj of parseJSONResponse<MyType>(response)) {
console.log("Parsed object:", obj);
}
Or compose the underlying Web Streams directly:
import {
JSONTextToSAJTransformStream,
SAJToObjectTransformStream,
} from "@simple-api-stream/json";
const response = await fetch("https://example.com/data.json");
const objects = response.body!
.pipeThrough(new TextDecoderStream())
.pipeThrough(new JSONTextToSAJTransformStream())
.pipeThrough(new SAJToObjectTransformStream<MyType>());
for await (const obj of objects) {
console.log("Parsed object:", obj);
}
SAJ Events
name |
Extra fields |
startDocument |
kind: "json" |
endDocument |
– |
error |
error: unknown (emitted when multiple: "json-seq" and errorToEvent: true) |
startObject / endObject |
– |
startArray / endArray |
– |
key |
key: string |
value |
type: "string" \| "number" \| "boolean" \| "null", value |
comment |
comment: string, style?: "line" \| "block" (emitted when allowComments: true) |
For full options, error classes, and usage examples, see the root README.
See Also
License
MIT