Created
June 29, 2012 16:51
-
-
Save rev087/3019160 to your computer and use it in GitHub Desktop.
Simple JSON parsing command line tool
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
#!/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