Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.