Skip to content

Instantly share code, notes, and snippets.

@noodlehaus
Created September 28, 2011 06:42
Show Gist options
  • Save noodlehaus/1247163 to your computer and use it in GitHub Desktop.
Save noodlehaus/1247163 to your computer and use it in GitHub Desktop.
CLI tool for getting values from JSON text files.
#!/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