to-ndjson
Convert a JSON array of objects to line-delimited JSON without parsing the entire array into memory
Last updated 5 months ago by danmactough .
MIT · Original npm · Tarball · package.json
$ cnpm install to-ndjson 
SYNC missed versions from official npm registry.

to-ndjson

Convert a JSON array of objects to line-delimited JSON without parsing the entire array into memory.

If you need to parse very large JSON that is unfortunately formatted as an array of objects, this is your jam.

Usage

All options of Readable and Writable streams can be passed to the ToNDJSON constructor, but the most relevant option is readableObjectMode. If you set readableObjectMode: true, your consumer will receive a stream of JSON objects rather. Otherwise, the default behavior is that your consumer will receive a stream of JSON stringified objects, each following by a newline.

const { ToNDJSON } = require('to-ndjson');
const { pipeline } = require('stream');
const fs = require('fs');
const { EOL } = require('os');

pipeline(
  fs.createReadStream(someHugeJsonArray),
  new ToNDJSON({ readableObjectMode: true }),
  async function* filter(lines) {
    for await (const line of lines) {
      if (line.property.match(/some test/)) {
        yield JSON.stringify(line) + EOL;
      }
    }
  },
  fs.createWriteStream(someOutputFile),
  (err) => {
    if (err) {
      console.error(err);
    } else {
      console.error('Done');
    }
  }
);

Prior art

This module depends on creationix/jsonparse by Tim Caswell and was inspired by dominictarr/JSONStream. In fact, the only reason to use this module instead of JSONStream (which can do the same thing and more) is to get compatibility and interoperability with the current Node streams ecosystem, such as stream.pipeline.

License

(The MIT License)

Current Tags

  • 1.0.1                                ...           latest (5 months ago)

2 Versions

  • 1.0.1                                ...           5 months ago
  • 1.0.0                                ...           5 months ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 0
Dependencies (1)
Dev Dependencies (2)
Dependents (0)
None

Copyright 2014 - 2016 © taobao.org |