Skip to content

Instantly share code, notes, and snippets.

@roppa
Created March 1, 2018 14:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save roppa/f827e23645d41ac61a84b1cf7f3580f1 to your computer and use it in GitHub Desktop.
Save roppa/f827e23645d41ac61a84b1cf7f3580f1 to your computer and use it in GitHub Desktop.
Executable script, takes in target json file, output json file, and a string of the json path you want to export, like 'parent.child.child'
#!/usr/bin/env node
const fs = require('fs');
const path = require('path');
const from = JSON.parse(fs.readFileSync(path.resolve(process.argv[2])).toString());
const to = path.resolve(process.argv[3]);
const jsonPath = process.argv[4].split('.');
function getValueFromPaths(paths, object) {
if (paths.length === 1) {
return object[paths[0]];
}
const attribute = paths.shift();
return getValueFromPaths(paths, object[attribute]);
}
fs.writeFileSync(to, JSON.stringify(getValueFromPaths(jsonPath, from)));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment