Skip to content

Instantly share code, notes, and snippets.

@rev087
Created June 29, 2012 16:51
Show Gist options
  • Save rev087/3019160 to your computer and use it in GitHub Desktop.
Save rev087/3019160 to your computer and use it in GitHub Desktop.
Simple JSON parsing command line tool
#!/usr/local/bin/node
/**
* Example usage:
*
* curl https://api.twitter.com/1/statuses/public_timeline.json | json 2.text
* curl https://api.twitter.com/1/statuses/public_timeline.json | json 0.user
*
*/
var util = require('util');
var buffer = '';
process.stdin.resume();
process.stdin.setEncoding('utf8');
process.stdin.on('data', function (chunk) {
buffer = buffer + chunk;
});
process.stdin.on('end', function () {
var obj = JSON.parse(buffer),
output = null;
if (process.argv.length > 2) {
var path = process.argv[2].split('.').reverse();
while (name = path.pop()) {
obj = obj[name];
}
}
if ('-h' in process.argv)
process.stdout.write(util.inspect(obj));
else
process.stdout.write(JSON.stringify(obj, null, ' '));
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment