csv to json npm code example

Example 1: create csv file nodejs

const csv = require('csv-parser');
const fs = require('fs');

fs.createReadStream('data.csv')
  .pipe(csv())
  .on('data', (row) => {
    console.log(row);
  })
  .on('end', () => {
    console.log('CSV file successfully processed');
  });

Example 2: convert json to csv npm

const { Parser } = require('json2csv'); const fields = ['field1', 'field2', 'field3'];const opts = { fields }; try {  const parser = new Parser(opts);  const csv = parser.parse(myData);  console.log(csv);} catch (err) {  console.error(err);}