-
-
Notifications
You must be signed in to change notification settings - Fork 53
Main module
The main module's default export is parserStream — a shortcut for parser.asStream(options) that returns the parser as a Duplex stream. The named export parser exposes the underlying parser factory.
import parserStream from 'stream-json';
import fs from 'node:fs';
const pipeline = fs.createReadStream('sample.json').pipe(parserStream());
let objectCounter = 0;
pipeline.on('data', data => data.name === 'startObject' && ++objectCounter);
pipeline.on('end', () => console.log(`Found ${objectCounter} objects.`));parserStream(options?) takes one optional argument: Parser options, and returns a Duplex stream. The writable side accepts text (Buffer/string), the readable side emits {name, value} token objects.
// parserStream is literally parser.asStream:
const parserStream = parser.asStream;In 2.x this entry was called make() and bundled an emit() decoration so consumers could subscribe to token-name events (startObject, keyValue, …) directly on the returned stream. In 3.x the bundled emit() is gone — compose it explicitly when you want the event API. See Migrating from 2.x to 3.x.
If you want token-name events on the stream, wrap with emit() from utils:
import parserStream from 'stream-json';
import emit from 'stream-json/utils/emit.js';
import fs from 'node:fs';
const pipeline = emit(fs.createReadStream('sample.json').pipe(parserStream()));
let objectCounter = 0;
pipeline.on('startObject', () => ++objectCounter);
pipeline.on('end', () => console.log(`Found ${objectCounter} objects.`));Re-exports the parser factory.
import {parser} from 'stream-json';
import fs from 'node:fs';
const pipeline = fs.createReadStream('sample.json').pipe(parser.asStream());
let objectCounter = 0;
pipeline.on('data', data => data.name === 'startObject' && ++objectCounter);
pipeline.on('end', () => console.log(`Found ${objectCounter} objects.`));The same value as the default export — provided as a named mirror so it composes with other named imports:
import {parserStream, parser} from 'stream-json';For Web Streams substrate, stream-json/web mirrors the main entry. The default export is parserWebStream, a shortcut for parser.asWebStream(options) that returns a {readable, writable} pair. The named export parser is the underlying parser factory from stream-json/web/parser.js.
import parserWebStream from 'stream-json/web';
import {parser} from 'stream-json/web';
const {readable, writable} = parserWebStream();
sourceReadable.pipeTo(writable);
for await (const tok of readable) console.log(tok);The Web entry pulls in no Node-stream imports — safe for browser bundles.
Start here
Core
Filters
Streamers
Essentials
Utilities
File I/O (Node-only)
JSONC
JSONL (use stream-chain)
Reference
Built on stream-chain