Created
September 28, 2011 06:42
-
-
Save noodlehaus/1247163 to your computer and use it in GitHub Desktop.
CLI tool for getting values from JSON text files.
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 | |
const fs = require('fs'); | |
var args = process.argv.slice(2); | |
if (args.length < 2) { | |
console.log('Usage: jsonv <jsonfile> <json-path-1> ... <json-path-n>'); | |
process.exit(1); | |
} | |
var data = JSON.parse(fs.readFileSync(args.shift())), | |
root = null, | |
segs = null, | |
leaf = null; | |
while (args.length) { | |
root = data; | |
segs = args.shift().split('.'); | |
for (var i = 0, n = segs.length; i < n; ++i) { | |
if (false == segs[i] in root) { | |
root = null; | |
console.log('not found: ' + segs[i]); | |
break; | |
} | |
root = root[segs[i]]; | |
} | |
if (root != null) { | |
console.log(root); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment