Skip to content
Eugene Lazutkin edited this page Jul 29, 2026 · 11 revisions

Signature: tokens{key, value} items

StreamObject assumes that a token stream represents an object and streams out its top-level properties as assembled JavaScript objects.

{"a": 1, "b": "a", "c": [{"lang": "de"}], "d": {"foo": null}, "e": true}
// StreamObject will produce an object stream:
{key: 'a', value: 1}
{key: 'b', value: 'a'}
{key: 'c', value: [{lang: 'de'}]}
{key: 'd', value: {foo: null}}
{key: 'e', value: true}

Only top-level properties are streamed — nested containers arrive fully assembled as that property's value.

As every streamer, it assumes that individual objects can fit in memory, but the whole file, or any other source, should be streamed.

Warning: it cannot be used with a stream of objects produced by a JSON Streaming source, or Pick if it picks more than one object. For a stream of multiple top-level values use StreamValues.

Introduction

import {streamObject} from 'stream-json/streamers/stream-object.js';
import fs from 'node:fs';

const pipeline = fs.createReadStream('sample.json').pipe(streamObject.withParserAsStream());

pipeline.on('data', data => console.log(data));

API

Being based on StreamBase, StreamObject has no special API.

Static methods and properties

streamObject(options)

streamObject() is the factory function. It takes options described above and returns a function for use in chain():

import chain from 'stream-chain';
import {parser} from 'stream-json';
import {streamObject} from 'stream-json/streamers/stream-object.js';

import fs from 'node:fs';

const pipeline = chain([fs.createReadStream('sample.json'), parser(), streamObject()]);

let objectCounter = 0;
pipeline.on('data', () => ++objectCounter);
pipeline.on('end', () => console.log(`Found ${objectCounter} objects.`));

streamObject.asStream(options)

Returns a Duplex stream (object-mode both sides) wrapping streamObject() for .pipe() usage.

withParser()

withParser() takes one argument:

  • options — combined Parser and streamer options. Passed to both the parser and streamObject().

Returns a Duplex stream (text-mode writable, object-mode readable) wrapping a parser() + streamObject() pipeline via stream-chain.

Built with the withParser() utility.

import {streamObject} from 'stream-json/streamers/stream-object.js';
import fs from 'node:fs';

const pipeline = fs.createReadStream('sample.json').pipe(streamObject.withParserAsStream());

let objectCounter = 0;
pipeline.on('data', () => ++objectCounter);
pipeline.on('end', () => console.log(`Found ${objectCounter} objects.`));

Web Streams

streamObject ships in two substrate-specific entries with the same factory shape:

  • Nodestream-json/streamers/stream-object.js. Has asStream, asWebStream, withParser, withParserAsStream, withParserAsWebStream.
  • Webstream-json/web/streamers/stream-object.js. Has asWebStream, withParser, withParserAsWebStream. Pulls in no Node-stream imports.

Both factories return the same flushable, so chain on either substrate auto-wraps it. Use chain from stream-chain on Node and from stream-chain/web on Web.

// Web
import {chain} from 'stream-chain/web';
import {parser} from 'stream-json/web/parser.js';
import {streamObject} from 'stream-json/web/streamers/stream-object.js';

const pipeline = chain([source, parser(), streamObject()]);
for await (const {key, value} of pipeline.readable) console.log(key, value);

Or the parser-included shortcut:

const {readable, writable} = streamObject.withParserAsWebStream();
sourceReadable.pipeTo(writable);
for await (const item of readable) console.log(item);

Clone this wiki locally