How to Convert CSV to JSON file having Comma Separated values in Node.js ?
A CSV is a comma-separated value file, identified by the .csv extension, is a format used for tabular data storage where values are separated by commas. This article presents a method to convert CSV data to JavaScript Object Notation (JSON) without any relaying on third-party npm packages. Unlike typical conversions, this approach handles scenarios where values within rows are also comma-separated, deviating from conventional column separation.
We will achieve this by first inputting the CSV file’s contents into an array and then splitting it based on a delimiter. Each row of the CSV will be transformed into JSON objects, which will be appended to a resultant array. Finally, this array will be converted to JSON format, resulting in the generation of a corresponding JSON output file. This process ensures a seamless conversion of CSV data into JSON format, catering to varying data structures without external dependencies.
Approach: Follow the steps below to achieve the solution:
- Read 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.
Convert the resultant array to JSON and generate the JSON output file.
Filename: app.js
// Reading the file using default
// fs npm package
const fs = require("fs");
csv = fs.readFileSync("CSV_file.csv")
// Convert the data to String and
// split it in an array
const array = csv.toString().split("\r");
// All the rows of the CSV will be
// converted to JSON objects which
// will be added to result in an array
let result = [];
// The array[0] contains all the
// header columns so we store them
// in headers array
let headers = array[0].split(", ")
// Since headers are separated, we
// need to traverse remaining n-1 rows.
for (let i = 1; i < array.length - 1; i++) {
let obj = {}
// Create an empty object to later add
// values of the current row to it
// Declare string str as current array
// value to change the delimiter and
// store the generated string in a new
// string s
let str = array[i]
let s = ''
// By Default, we get the comma separated
// values of a cell in quotes " " so we
// use flag to keep track of quotes and
// split the string accordingly
// If we encounter opening quote (")
// then we keep commas as it is otherwise
// we replace them with pipe |
// We keep adding the characters we
// traverse to a String 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
}
// Split the string using pipe delimiter |
// and store the values in a properties array
let properties = s.split("|")
// For each header, if the value contains
// multiple comma separated data, then we
// store it in the form of array otherwise
// directly the value is stored
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]
}
// Add the generated object to our
// result array
result.push(obj)
}
// Convert the resultant array to json and
// generate the JSON output file.
let json = JSON.stringify(result);
fs.writeFileSync('output.json', json);
Input:
Output: