Skip to content

Instantly share code, notes, and snippets.

@tlync
Created September 1, 2012 06:08
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save tlync/3565248 to your computer and use it in GitHub Desktop.
Save tlync/3565248 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++
}
})
@korrio
Copy link

korrio commented Jul 15, 2020

how to use this ?

@btray77
Copy link

btray77 commented May 27, 2022

node split-json.js .json

@itsP33t
Copy link

itsP33t commented Jun 17, 2022

Love it!

@harrykimpel
Copy link

I was not aware of this, thanks for sharing!

@mulhoon
Copy link

mulhoon commented Nov 14, 2023

11 years later... Thanks 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment