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.

Leave a Reply

Your email address will not be published.

If you like the content of this blog, subscribe to my email list to get exclusive articles not available to anyone else.