json-street
Simple and efficient streaming JSON processor.
npm install json-street;
Process JSON data incrementally without waiting for it to be fully received:
import * as jsonst from 'json-street;
const response = await fetch('https:
const stream = response.body.pipeThrough(new TextDecoderStream());
const items = [];
await jsonst.visit(stream, {
items: item => items.push(item),
});
Specify the expected type of the data to ensure the visitor is appropriately typed:
type Data = {
items: {
id: string;
name: string;
metadata: unknown;
}[];
};
await jsonst.visit<Data>(stream, )
Use nested visitors to process complex schemas:
type User = {
id: string;
posts: {
title: string;
comments: {
author: string;
text: string;
}[];
}[];
};
const titles = [];
const comments = [];
await jsonst.visit<User>(stream, {
posts: jsonst.array({
title: t => titles.push(t),
comments: jsonst.array(c => comments.push(c)),
})
});