Skip to content

Instantly share code, notes, and snippets.

@JosePedroDias
Last active January 5, 2020 13:37
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 JosePedroDias/1b02f75966434c8bd8385440af529ab8 to your computer and use it in GitHub Desktop.
Save JosePedroDias/1b02f75966434c8bd8385440af529ab8 to your computer and use it in GitHub Desktop.
traverse to manipulate JSON
// yarn list --json > modules.json
const fs = require('fs');
const O = require('./modules.json');
function visit(o) {
if (typeof o === 'object' && o !== null) {
if (o instanceof Array) {
// change array (noop)
for (const it of o) {
visit(it);
}
} else {
// change object
delete o.shadow;
delete o.color;
delete o.hint;
delete o.depth;
for (const k of Object.keys(o)) {
visit(o[k]);
}
}
}
}
visit(O);
fs.writeFileSync('modules2.json', JSON.stringify(O.data.trees, null, 2));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment