Skip to content
Eugene Lazutkin edited this page Mar 16, 2026 · 13 revisions

StreamValues assumes that a token stream represents subsequent values and stream them out one by one.

1 "a" [] {} true
// StreamValues will produce an object stream:
{key: 0, value: 1}
{key: 1, value: 'a'}
{key: 2, value: []}
{key: 3, value: {}}
{key: 4, value: true}

StreamValues is a usual companion for Pick, which streams selected subobjects individually.

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

Introduction

The prelude:

const {streamValues} = require('stream-json/streamers/stream-values.js');
const fs = require('fs');

Take a JSON streaming pipeline and pipe it into the parser:

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

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

Alternatively, first create the streaming pipeline and then pipe JSON messages into it:

const q = streamValues.withParserAsStream();
q.on('data', data => console.log(data));
fs.createReadStream('sample_a.json').pipe(q);

API

Being based on StreamBase, StreamValues has no special API.

Static methods and properties

streamValues(options) and make(options)

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

const {chain} = require('stream-chain');
const {parser} = require('stream-json');
const {streamValues} = require('stream-json/streamers/stream-values.js');

const fs = require('fs');

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

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

withParser()

withParser() takes one argument:

  • options is an object described in Parser's options. It is used to initialize both streams (a Parser instance and a stream returned by make()).
    • jsonStreaming is always set to true because otherwise, it doesn't make sense to stream just one object.

It returns a stream produced by stream-chain, which wraps the pipeline. The most important utility of withParser() is that it correctly sets object modes of the returned stream: object mode for the Readable part and text mode for the Writable part.

This static method is created using withParser() utility. It simplifies a case when a stream should be immediately preceded by a parser.

const {streamValues} = require('stream-json/streamers/stream-values.js');
const fs = require('fs');

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

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

Clone this wiki locally