Published on

How to convert CSV to JSON in Node.js

Authors

Most of the time, file imports usually consists of CSV files.

And we need to process them in our application. However, converting them to JSON object gives us more flexibility than CSV string.

Let's see how to do that in Node.js

Install the Dependencies

npm install neat-csv

Snippet

import neatCsv from "neat-csv";
const rawJson = await neatCsv(`CSV file's content as string here`);

console.log({rawJson});

Example - Reading from file

import neatCsv from "neat-csv";
import fs from "fs/promises";

// Read CSV file
const csvString = await fs.readFile("path/to/csv/file.csv", "utf-8");

// Convert CSV to JSON Object
const jsonObject = await neatCsv(csvString);

Happy playing-with CSV!