Categories
JavaScript Answers

How to convert CSV to JSON in Node.js and JavaScript?

Spread the love

Sometimes, we want to convert CSV to JSON in Node.js and JavaScript.

In this article, we’ll look at how to convert CSV to JSON in Node.js and JavaScript.

How to convert CSV to JSON in Node.js and JavaScript?

To convert CSV to JSON in Node.js and JavaScript, we use the csvtojson library.

To install it, we run

npm i csvtojson

Then we use it by writing

const csv = require("csvtojson");

const csvFilePath = "customer-data.csv";
const jsonObj = await csv().fromFile(csvFilePath);
console.log(jsonObj);

to call fromFile with the csvFilePath to read the csv file and return a promise with the csv data parsed into a JavaScript object.

And then we use await to get the data from the returned promise.

Conclusion

To convert CSV to JSON in Node.js and JavaScript, we use the csvtojson library.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *