$ cnpm install stream-json
stream-json is a micro-library of Node.js stream components for creating custom JSON processing pipelines with a minimal memory footprint. It can parse JSON files far exceeding available memory. Even individual data items (keys, strings, and numbers) can be streamed piece-wise. A SAX-inspired event-based API is included.
Components:
emit().pick()).{key, value} objects. Faster than parser({jsonStreaming: true}) + streamValues() when items fit in memory.disassembler() + stringer().All components are building blocks for custom data processing pipelines. They can be combined with each other and with custom code via stream-chain.
Distributed under the New BSD license.
const {chain} = require('stream-chain');
const {parser} = require('stream-json');
const {pick} = require('stream-json/filters/pick.js');
const {ignore} = require('stream-json/filters/ignore.js');
const {streamValues} = require('stream-json/streamers/stream-values.js');
const fs = require('fs');
const zlib = require('zlib');
const pipeline = chain([
fs.createReadStream('sample.json.gz'),
zlib.createGunzip(),
parser(),
pick({filter: 'data'}),
ignore({filter: /\b_meta\b/i}),
streamValues(),
data => {
const value = data.value;
// keep data only for the accounting department
return value && value.department === 'accounting' ? data : null;
}
]);
let counter = 0;
pipeline.on('data', () => ++counter);
pipeline.on('end', () => console.log(`The accounting department has ${counter} employees.`));
See the full documentation in Wiki.
Companion projects:
stream-json:
rows as arrays of string values. If a header row is used, it can stream rows as objects with named fields.npm install --save stream-json
# or: yarn add stream-json
The library is organized as small composable components based on Node.js streams and events. The source code is compact — read it to understand how things work and to build your own components.
Bug reports, simplifications, and new generic components are welcome — open a ticket or pull request.
stream-chain 3.x, bundled TypeScript definitions. New: JSONC parser/stringer, FlexAssembler. See Migrating from 1.x to 2.x.The full history is in the wiki: Release history.
Copyright 2013 - present © cnpmjs.org | Home |