Created
September 5, 2011 05:46
-
-
Save justgord/1194181 to your computer and use it in GitHub Desktop.
handy command line util for looking into JSON files - based on JSONPath npm module
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 | |
var fs = require('fs'); | |
var jsonpath = require('JSONPath'); | |
if (process.argv.length<3) | |
{ | |
console.log('usage: jsonpath json_filename json_path_expr'); | |
exit; | |
} | |
var sfile = process.argv[2]; | |
var spath = process.argv[3]; | |
var cont = fs.readFileSync(sfile); | |
var obj = JSON.parse(cont); | |
var res = jsonpath.eval(obj, spath); | |
console.log(res); |
Here's a quick fix if your result object has too many layers of nesting, and console.log truncates them with [Object]. Change the last line to:
console.log(JSON.stringify(res));
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Nice, short code. It would be cool if this could be adapted to be used as a pipe, such that if another app is piping a stream of json data to it, the json_filename would not be used, instead getting the data from the stream. I've never used Node, would that be an easy change?