Skip to content

Instantly share code, notes, and snippets.

@PaulMougel
Created November 15, 2013 22:45
Show Gist options
  • Save PaulMougel/7493000 to your computer and use it in GitHub Desktop.
Save PaulMougel/7493000 to your computer and use it in GitHub Desktop.
Sample stream usage in Node.js: parse a JSON array on-the-fly
var stream = require('stream');
var JSONStream = require('JSONStream');
var inputStream = new stream.PassThrough();
var jsonStream = JSONStream.parse('*');
var finalStream = new stream.Writable({ objectMode: true });
finalStream._write = function (doc, encoding, done) {
console.log(doc);
done();
};
inputStream.pipe(jsonStream).pipe(finalStream);
inputStream.push('[{"a": 1},{"b" : 2},{"c": 3},');
inputStream.push('{"d": [4,5,6]}]');
inputStream.end();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment