Created
April 21, 2020 06:39
-
-
Save saikatharryc/b5c08eefa6e455f32e8700982e849a2e to your computer and use it in GitHub Desktop.
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 path = require('path'); | |
//List all your folders you want to merge | |
// This will create One single merged JSON file named merged.json inside each folder. | |
// NOTE: this requires all the file must be having Array. incase not you can handle the cases in line 13 Here. | |
const folders = [ | |
'/Users/saikatharryc/Documents/testing' | |
] | |
const init = (dirz)=> { | |
return fs.readdirSync(dirz) | |
.filter(name => path.extname(name) === '.json') | |
.map(name =>require(path.join(dirz, name))); | |
} | |
const main = ()=>{ | |
folders.forEach(f=>{ | |
arrays = init(f) | |
// console.log(arrays[0]) | |
var merged = [].concat.apply([], arrays); | |
fs.writeFileSync(path.join(f,'merged.json' ),JSON.stringify(merged, null, 4)); | |
console.log(f+' : '+merged.length) | |
}) | |
} | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment