Last active
August 29, 2015 14:08
-
-
Save robinsk/7bb33702ba07d8f94894 to your computer and use it in GitHub Desktop.
JSON-query?
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
[ | |
{ | |
"id": 1, | |
"name": "Item number 1" | |
}, | |
{ | |
"id": 2, | |
"name": "A second item" | |
} | |
] |
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/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); | |
}); |
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
$ 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