Monday, 10 April 2017

CSV parsing with nodejs and csvtojson plugin

i have a csv

name,age,phonenumber,time arun,27,923444,1 arun,27,999999,2

if i see the name arun for second time i need to skip the second one. how can i do it.

i use node js and csv to json

and my end json should only have [{'name':'arun','age':'27','phonenumber':'923444','time':'1' }]

var Converter=require("csvtojson").Converter;
var parse = require('csv-parse');
var processedRecords=[];
var csvData=[];
var i=0;
function processRecords() {
    var csvConverter=new Converter({constructResult:false, toArrayString:true}); // The constructResult parameter=false will turn off final result construction in memory for stream feature. toArrayString will stream out a normal JSON array object.

var readStream=require("fs").createReadStream("/home/ubuntu/Documents/hello/123/idocuments/samplecsvfile.csv");

var writeStream=require("fs").createWriteStream("/home/ubuntu/Documents/hello/123/idocuments/outputData1.json");
readStream.pipe(csvConverter).on('csv',function(csvrow){
console.log(csvrow[0]);
if(csvrow.length>0){
if(csvrow[0]!=null){
if(processedRecords.indexOf(csvrow[0])==-1){
console.log('index of '+ processedRecords.indexOf(csvrow[0]));
processedRecords.push(csvrow[0]);
csvData.push(csvrow);
}
}
}
}).on('done',function() {//do something wiht csvData
console.log(csvData);
}).pipe(writeStream);
//readStream.pipe(csvConverter).pipe(writeStream);
}



via Praveen

No comments:

Post a Comment