Skip to content

Instantly share code, notes, and snippets.

@1wheel
Created January 21, 2017 00:11
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 1wheel/b4982b8c8bff7d640df4aab5e8524425 to your computer and use it in GitHub Desktop.
Save 1wheel/b4982b8c8bff7d640df4aab5e8524425 to your computer and use it in GitHub Desktop.
ndjson-to-file

Trying to save a big blob of JSON that crashes JSON.stringify. Spliting into smaller arrays and saving works, but I can't figure out how to use a stream.

var fs = require('fs')
//generate fake data
var data = [
{foo: 1},
{bar: 3},
]
var bigData = [
data,
data,
data,
data,
data
]
var count = 100100
for (var i = 0; i < count; i++){
bigData.push(data)
}
//write out 10 blobs of json. works but isn't too pretty...
var maxItems = Math.floor(count/10)
var i = 0
while (i*maxItems < count){
var dataSlice = JSON.stringify(bigData.slice(i*maxItems, (i + 1)*maxItems))
fs.writeFileSync(i + '.json', dataSlice)
i++
}
//this breaks - don't think i'm piping to the wstream correctly?
var ndjson = require('ndjson')
var serialize = ndjson.serialize()
var wstream = fs.createWriteStream('myfile.json');
serialize.on('data', function(line) {
// line is a line of stringified JSON with a newline delimiter at the end
// console.log(line)
})
serialize.write(bigData)
serialize.pipe(wstream)
serialize.end()
// this also breaks
var JSONStream = require('JSONStream');
var es = require('event-stream');
var fs = require('fs');
var obj = {};
for (var i = 0; i < 2000; i++) {
obj['prop' + i] = 'value' + i;
}
var out = fs.createWriteStream(__dirname + '/out.json');
es.readable(function (count, next) {
// for (var key in obj) {
// this.emit('data', [key, obj[key]]);
// }
var that = this
bigData.forEach(function(d){
that.emit('data', d)
})
that.emit('end');
next();
}).pipe(JSONStream.stringify()).pipe(out);
@cpietsch
Copy link

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