To convert CSV to JSON in Node.js, we use the csvtojson
module.
We install it by running
npm install --save csvtojson@latest
Then we use it by writing
const csv = require("csvtojson");
const jsonArrayObj = await csv().fromFile(csvFilePath);
console.log(jsonArrayObj);
to call fromFile
to read the csv file into an JSON array.
Then we use await
to get the JSON array of objects from the returned promise.