Created
June 14, 2020 11:37
-
-
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.
This file contains hidden or 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
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