Skip to content

Instantly share code, notes, and snippets.

@aaronpowell
Created August 14, 2014 23:40
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aaronpowell/7dba84a0020c546aaad2 to your computer and use it in GitHub Desktop.
Save aaronpowell/7dba84a0020c546aaad2 to your computer and use it in GitHub Desktop.
jq - a simple cli for JSON querying
process.stdin.setEncoding('utf8');
var what = process.argv[2] || '';
process.stdin.on('readable', function () {
var chunk = process.stdin.read();
if (chunk) {
var obj = JSON.parse(chunk);
switch(what) {
case '.':
return console.log(obj);
case '':
return console.log('nothing to do');
default:
var split = what.split('.').filter(function (s) { return !!s; });
return console.log(
split.reduce(function (obj, next) {
return obj == undefined ? undefined: obj[next];
}, obj)
);
}
}
});
process.stdin.on('end', function () {
process.stdout.write('dooooooooone\n');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment