Skip to content

Instantly share code, notes, and snippets.

@nicolashery
Created November 29, 2013 14:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nicolashery/7706499 to your computer and use it in GitHub Desktop.
Save nicolashery/7706499 to your computer and use it in GitHub Desktop.
Working with JSON using Node.js, Streams, and the Unix command line
// Usage:
// cat customers_raw.json | node process_customers > customers.json
// https://github.com/dominictarr/JSONStream
var JSONStream = require('JSONStream');
// https://github.com/rvagg/through2
var through2 = require('through2');
process.stdin
.pipe(JSONStream.parse('*'))
.pipe(through2({objectMode: true}, function(data, encoding, callback) {
// Do something cool with the `data` object
this.push(data)
callback();
}))
.pipe(JSONStream.stringify('[\n', ',\n', '\n]\n'))
.pipe(process.stdout);
// If we use `head` downstream it will send an error signal
process.stdout.on('error', process.exit);
// Also check out command line tool json(1):
// http://trentm.com/json
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment