Skip to content

Instantly share code, notes, and snippets.

@RahulJyala7
Forked from tlync/split-json.js
Created March 22, 2019 19:36
Show Gist options
  • Select an option

  • Save RahulJyala7/2e2414ff8f2e391f43a9ee72a7a7482d to your computer and use it in GitHub Desktop.

Select an option

Save RahulJyala7/2e2414ff8f2e391f43a9ee72a7a7482d to your computer and use it in GitHub Desktop.
Split large json file
if(process.argv.length < 3){
console.log('target file path is required.')
process.exit(1)
}
var target = process.argv[2]
console.log('file: ' + target)
var fs = require('fs')
fs.readFile(target, function (err, data) {
if (err) throw err
var jsonArray = JSON.parse(data),
i = 1
while(jsonArray.length !== 0){
var fileName = target + '.' + i
fs.writeFileSync(fileName, JSON.stringify(jsonArray.splice(0, 500)))
console.log(fileName)
i++
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment