Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- a,b,c,d
- 1,2,3,4
- 5,6,7,8
- ...
- [
- {"a": 1,"b": 2,"c": 3,"d": 4},
- {"a": 5,"b": 6,"c": 7,"d": 8},
- ...
- ]
- var es = require('event-stream')
- es.pipeline( //connect streams together with `pipe`
- process.openStdin(), //open stdin
- es.split(), //split stream to break on newlines
- es.map(function (data, callback) { //turn this async function into a stream
- callback(null
- , JSON.stringify(parseCSVLine(data))) // deal with one line of CSV data
- }),
- process.stdout
- )
- var Converter=require("csvtojson").core.Converter;
- var csvConverter=new Converter(false); // The parameter false will turn off final result construction. It can avoid huge memory consumption while parsing. The trade off is final result will not be populated to end_parsed event.
- var readStream=require("fs").createReadStream("inputData.csv");
- var writeStream=require("fs").createWriteStream("outpuData.json");
- var started=false;
- csvConverter.on("record_parsed",function(rowJSON){
- if (started){
- writeStream.write(",n");
- }
- writeStream.write(JSON.stringify(rowJSON)); //write parsed JSON object one by one.
- if (started==false){
- started=true;
- }
- });
- writeStream.write("[n"); //write array symbol
- csvConverter.on("end_parsed",function(){
- writeStream.write("n]"); //end array symbol
- });
- csvConverter.from(readStream);
- csvtojson myCSVFile.csv
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement