Last active
June 29, 2016 03:21
-
-
Save arahmanali/4bdd4272100c6befe6429276710e0947 to your computer and use it in GitHub Desktop.
This file contains 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
/////////////////////////////////////////////////////// | |
// $ node txt-to-json-parser.js [file_you_want_to_parse] | |
/////////////////////////////////////////////////////// | |
var fs = require('fs'); | |
var arr = [] | |
var file_name = process.argv[2]; | |
var stream = fs.createReadStream(file_name + '.txt', { flags: 'r+', encoding: 'utf-8' }); | |
var buf = ''; | |
stream.on('data', function(d) { | |
buf += d.toString(); // when data is read, stash it in a string buffer | |
pump(); // then process the buffer | |
}); | |
function pump() { | |
var pos; | |
while ((pos = buf.indexOf('\n')) >= 0) { // keep going while there's a newline somewhere in the buffer | |
if (pos == 0) { // if there's more than one newline in a row, the buffer will now start with a newline | |
buf = buf.slice(1); // discard it | |
continue; // so that the next iteration will start with data | |
} | |
processLine(buf.slice(0, pos)); // hand off the line | |
buf = buf.slice(pos + 1); // and slice the processed data off the buffer | |
} | |
} | |
function processLine(line) { // here's where we do something with a line | |
var data = line.split(','); | |
var obj = {}; | |
obj["key"] = data[0] | |
arr.push(obj); | |
if (arr.length == 499999) { // file upto 500k lines | |
write(file_name+'.json', JSON.stringify(arr, null, 4)) | |
} | |
} | |
function write(path, data) { | |
fs.writeFile(path, data, function(err) { | |
if (err) return console.log(err); | |
console.log('File Written > ' + file_name + '.json'); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment