Skip to content
Eugene Lazutkin edited this page May 23, 2026 · 11 revisions

Filter is a token item filter based on FilterBase. It selects objects from a stream ignoring the rest. Its filter is called for all value tokens. A shape of incoming stream is preserved: when selecting a subobject, all parent objects will be recreated to keep a stream valid.

In order to recreate parent objects correctly this filter requires that an upstream produced packed keys (keyValue tokens).

Introduction

const {filter} = require('stream-json/filters/filter.js');
const fs = require('fs');

// our data stream:
// {total: 123456789, meta: {...}, data: [...]}
// we want to remove all properties but 'data':
// {data: [...]}

const pipeline = fs.createReadStream('sample.json').pipe(filter.withParserAsStream({filter: 'data'}));

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

API

Filter has no special API. Based on FilterBase it uses the following options:

  • filter
    • If it returns a truthy value, the current object is streamed out.
      • If we have any unselected parent objects, they will be recreated.
      • Any subobject will have a chance to be filtered.
    • It is called only for the following tokens: startObject, startArray, startString, startNumber, stringValue, numberValue, nullValue, trueValue, falseValue.
  • pathSeparator
  • once

See their definitions in FilterBase.

Static methods and properties

filter(options)

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

const {chain} = require('stream-chain');
const {parser} = require('stream-json/parser.js');
const {filter} = require('stream-json/filters/filter.js');

const fs = require('fs');

const pipeline = chain([fs.createReadStream('sample.json'), parser(), filter({filter: 'data'})]);

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

filter.asStream(options)

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

withParser()

withParser() takes one argument:

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

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

Built with the withParser() utility.

const {filter} = require('stream-json/filters/filter.js');
const fs = require('fs');

const pipeline = fs.createReadStream('sample.json').pipe(filter.withParserAsStream({filter: 'data'}));

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

Web Streams

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

  • Nodestream-json/filters/filter.js. Has asStream, asWebStream, withParser, withParserAsStream, withParserAsWebStream.
  • Webstream-json/web/filters/filter.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 {filter} from 'stream-json/web/filters/filter.js';

const pipeline = chain([source, parser.asWebStream(), filter({filter: 'data'})]);
for await (const tok of pipeline.readable) console.log(tok);

Or use the parser-included shortcut:

import {filter} from 'stream-json/web/filters/filter.js';

const {readable, writable} = filter.withParserAsWebStream({filter: 'data'});
sourceReadable.pipeTo(writable);
for await (const tok of readable) console.log(tok);

Clone this wiki locally