JavaScript Answers Nodejs

How to Convert CSV to JSON in Node.js?

Spread the love

To convert CSV to JSON in our Node.js apps, we can use the csvtojson library.

To install it, we run:

npm i csvtojson

Then we if have the following in sample.csv :


then we can convert the CSV file content into a JSON object by writing:

const csv = require("csvtojson");
const csvFilePath = './sample.csv'

const convertToJson = async (csvFilePath) => {
  const jsonArrayObj = await csv().fromFile(csvFilePath)


We have the convertToJson function that takes a csvFilePath .

Then we call csv().fromFile with it to convert the CSV file content into a JSON object and assign that to jsonArrayObj .

As a result, jsonArrayObj is:

  { a: '1', b: '2', c: '3', d: '4' },
  { a: '5', b: '6', c: '7', d: '8' }

when we call it.

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 *