-
-
Save rctay/25c62af2dfb52d7b2335 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 | |
// Reads JSON from stdin, and runs a JSONPath expression from the command-line on it. | |
// | |
// eg. | |
// $ npm install # install dependencies | |
// $ echo '{"store": {"book":[{"category":"fiction"}]}}' | node jsonpath.js '$.store.book[0].category' | |
// fiction | |
var stdin = process.stdin, | |
stdout = process.stdout, | |
inputChunks = []; | |
var jsonPath = require('JSONPath'); | |
stdin.resume(); | |
stdin.setEncoding('utf8'); | |
stdin.on('data', function (chunk) { | |
inputChunks.push(chunk); | |
}); | |
stdin.on('end', function () { | |
var inputJSON = inputChunks.join(), | |
parsedData = JSON.parse(inputJSON); | |
var result = jsonPath.eval(parsedData, process.argv[2], {wrap: false}) | |
stdout.write(result.toString()); // could be a number | |
stdout.write('\n'); | |
}); |
This file contains hidden or 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
{ | |
"dependencies": { | |
"JSONPath": "0.10.x" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment