Created
November 15, 2013 22:45
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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