Created
March 1, 2018 14:49
-
-
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'
This file contains 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'); | |
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