Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how can I solve json2csv header and data problem? Node js

i read the json2csv documentation and applied it in my program

const {parse} = require('json2csv')

 header = [customerNo,Name,BranchCode,representive,overall]
 list = [{cNo:1,name:Jack,bCode:3,rps:Bill,overall},{cNo:2,name:Justin,bCode:2,rps:Wade,overall:180}]

    const fields = header;
    const opts = { fields };
    try {
        var csv = parse(list,opts);
          csv=csv.replace(/,/g,";")
        
     
          console.log(csv);
      } catch (err) {
        console.log(err)
         
      

  
}

result = customerNo;Name;BranchCode;representive;overall

My data not coming under headers. Why I have this problem. Am I doing something wrong ?

like image 430
safa aytan Avatar asked Aug 07 '26 20:08

safa aytan


1 Answers

Since your desired column names are different than the property keys you need to define them as custom:

const fields = [{
        label: 'customerNo',
        value: 'cNo'
    }, {
        label: 'Name',
        value: 'name'
    }, {
        label: 'BranchCode',
        value: 'bCode'
    }, {
        label: 'representive',
        value: 'rps'
    }, {
        label: 'overall',
        value: 'overall'
    }
];

This is also explained in the docs -> https://github.com/zemirco/json2csv#example-3

like image 197
eol Avatar answered Aug 09 '26 11:08

eol



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!