1// Install
2npm i csvtojson
3
4// From CSV File to JSON Array
5/** csv file
6a,b,c
71,2,3
84,5,6
9*/
10const csvFilePath='<path to csv file>'
11const csv=require('csvtojson')
12csv()
13.fromFile(csvFilePath)
14.then((jsonObj)=>{
15 console.log(jsonObj);
16 /**
17 * [
18 * {a:"1", b:"2", c:"3"},
19 * {a:"4", b:"5". c:"6"}
20 * ]
21 */
22})
1const { 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);}