Skip to content

Instantly share code, notes, and snippets.

@robinsk
Last active August 29, 2015 14:08
Show Gist options
  • Save robinsk/7bb33702ba07d8f94894 to your computer and use it in GitHub Desktop.
Save robinsk/7bb33702ba07d8f94894 to your computer and use it in GitHub Desktop.
JSON-query?
[
{
"id": 1,
"name": "Item number 1"
},
{
"id": 2,
"name": "A second item"
}
]
#!/usr/bin/env node
var vm = require('vm');
var input = '';
var command = process.argv.slice(2).join(' ');
process.stdin.setEncoding('utf8');
process.stdin.on('readable', function() {
var chunk = process.stdin.read();
if (chunk !== null) {
input += chunk;
}
});
process.stdin.on('end', function() {
var sandbox = {
'$': JSON.parse(input),
'log': console.log
};
vm.runInNewContext(command, sandbox);
});
$ cat 1_input.json | ./2_json-query.js "$.forEach(function(val) { log('name: ' + val.name); })"
name: Item number 1
name: A second item
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment