node-csvtojson icon indicating copy to clipboard operation
node-csvtojson copied to clipboard

CSV file has duplicate headers but json doesn't have duplicate column. Is there any option to change this behavior.

Open aniltomar10 opened this issue 5 years ago • 6 comments

aniltomar10 avatar Dec 06 '18 21:12 aniltomar10

Hi, can you give an example?

~Keyang

Keyang avatar Dec 06 '18 21:12 Keyang

@Keyang Example: csv file has header: firstName, lastName, firstName values: [ abc,test,xyz]

Json result:

{ lastName: 'test', firstName: 'xyz' }

This creates problem for me because I wouldn't know if file had duplicate columns.

I am handling this using 'header" event like this:

let duplicateDetected=false; stream.pipe( csv({delimiter:'auto'}).on('error', (err) => { //handle error }).on('header',(header)=>{ //check for duplicate duplicateDetected=true; } }) ).then((jsonData) => { if(!duplicateDetected){ //continue logic } });

I am assuming then function will be called at the end not before setting 'header' event. Is this assumption correct?

aniltomar10 avatar Dec 06 '18 22:12 aniltomar10

Any news on this issue? I have the same problem. Duplicated headers as my source CSV transform multiple values in duplicated headers.

Thanks !

tiagocardoso avatar May 19 '19 22:05 tiagocardoso

I have the same issue and would like to have this fixed. Also what about blank headers?

CharlesDeJager avatar Apr 23 '20 22:04 CharlesDeJager

@Keyang has replied about how to handle such scenario in this issue: https://github.com/Keyang/node-csvtojson/issues/217 The idea is to gather the duplicate headers by tapping into the "header" event and then using that to change the headers that get used as JSON keys using the "csv" approach.

Inder2108 avatar Aug 31 '20 09:08 Inder2108

@Keyang , I would like to contribute to this bug fix by adding a change to rename the Duplicate Headers with their count appended to it. I have a fix ready locally but would like to have your feedback on the approach before proceeding with a Pull request.

Inder2108 avatar Sep 01 '20 16:09 Inder2108