Created
November 29, 2013 14:32
-
-
Save nicolashery/7706499 to your computer and use it in GitHub Desktop.
Working with JSON using Node.js, Streams, and the Unix command line
This file contains 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
// 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