Skip to content

Instantly share code, notes, and snippets.

@jbnv
Created June 14, 2020 11: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 jbnv/eb5bf3bc40019ec7a483b56b2d6ca45e to your computer and use it in GitHub Desktop.
Save jbnv/eb5bf3bc40019ec7a483b56b2d6ca45e to your computer and use it in GitHub Desktop.
Node script to split a JSON file containing a single object into a folder of files.
const fs = require('fs');
const directory = process.argv[2];
fs.mkdir(directory, {
recursive: false
}, (err) => {
fs.readFile(`./${directory}.json`, 'utf8', (err, data) => {
if (err) throw err;
Object.entries(JSON.parse(data)).forEach(pair => {
const key = pair[0];
const value = pair[1];
fs.writeFileSync(`./${directory}/${key}.json`, JSON.stringify(value), 'utf8');
console.log(key);
})
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment