How To Convert CSV to JSON file Having Comma Separated Values In NodeJS? Last Updated : 07 Jan, 2025 Comments Improve Suggest changes Like Article Like Report To convert a CSV file into a JSON file in Node.js with comma-separated values, follow a structured approach that utilizes the fs module to read the file and process the data manually. Here's how you can achieve the solutionApproachRead the CSV file using the default fs npm package.Convert the data to String and split it in an array.Generate a headers array.For all the remaining n-1 rows do the following: Create an empty object to add values of the current row to it.Declare a string str as the current array value to change the delimiter and store the generated string in a new string s.If we encounter an opening quote (") then we keep commas as it is otherwise we replace them with pipe "|"Keep adding the characters we traverse to a String s.Split the string using pipe delimiter | and store the values in a properties array.For each header, if the value contains multiple comma-separated data, then we store it in the form of an array otherwise directly the value is stored.Add the generated object to our result array.Example:Convert the resultant array to JSON and generate the JSON output file. javascript //app.js/ const fs = require("fs"); csv = fs.readFileSync("CSV_file.csv") const array = csv.toString().split("\r"); let result = []; let headers = array[0].split(", ") for (let i = 1; i < array.length - 1; i++) { let obj = {} let str = array[i] let s = '' let flag = 0 for (let ch of str) { if (ch === '"' && flag === 0) { flag = 1 } else if (ch === '"' && flag == 1) flag = 0 if (ch === ', ' && flag === 0) ch = '|' if (ch !== '"') s += ch } let properties = s.split("|") for (let j in headers) { if (properties[j].includes(", ")) { obj[headers[j]] = properties[j] .split(", ").map(item => item.trim()) } else obj[headers[j]] = properties[j] } result.push(obj) } let json = JSON.stringify(result); fs.writeFileSync('output.json', json); Explanation: This code reads a CSV file using Node.js’s fs module and converts it to a JSON format. It starts by reading the file and splitting it into an array of rows. The first row, which contains the headers, is separated and used as keys for the JSON objects. Each subsequent row is processed by handling quotes and commas properly, creating an object for each row, and adding it to a result array. Finally, the result array is converted into a JSON string and saved to a file.Output:Convert CSV to JSON file Having Comma Separated Values In NodeJS Comment More infoAdvertise with us Next Article How To Convert CSV to JSON file Having Comma Separated Values In NodeJS? K karankc27 Follow Improve Article Tags : JavaScript Web Technologies Node.js CSV JSON Node.js-Misc +2 More Similar Reads How to convert a 2D array to a comma-separated values (CSV) string in JavaScript ? Given a 2D array, we have to convert it to a comma-separated values (CSV) string using JS. Input:[ [ "a" , "b"] , [ "c" ,"d" ] ]Output:"a,b c,d"Input:[ [ "1", "2"]["3", "4"]["5", "6"] ]Output:"1,23,45,6"To achieve this, we must know some array prototype functions which will be helpful in this regard 4 min read How to Convert CSV to JSON file and vice-versa in JavaScript ? CSV files are a common file format used to store data in a table-like manner. They can be particularly useful when a user intends to download structured information in a way that they can easily open and read on their local machine. CSV files are ideal for this because of their portability and unive 3 min read How to Convert CSV to Excel in Node.js ? NodeJS has become one of the famous backend frameworks for development. So in this article, we'll see one of its use to convert CSV into Excel We will use CSVtoExcel npm package to do the conversion of files. It provides convertCsvToXlsx function to implement the conversion. convertCsvToXlsx(source, 2 min read How to Convert CSV to JSON in ReactJS ? Dealing with data in various formats is a common task in web development. CSV (Comma Separated Values) and JSON (JavaScript Object Notation) are two widely used formats for storing and transmitting data. Converting CSV to JSON is often required when working with data in ReactJS applications. Approac 4 min read How to Read and Write Excel file in Node.js ? Read and write excel file in Node is a common task while updating the data from backend. There are many packages available on npm for performing these operations.Approach To read and write Excel file in Node js we will use the xlsx package from NPM. We can read the excel file using readFile method a 4 min read Like