convert csv to json file in node js

How to Convert CSV to JSON File in Node.JS

Inspecting a CSV file and parsing it to JSON is not a difficult task in Node.JS. Comma-Separated Values (CSV) is defined as a data exchange format that collects data in a tabular format. It is a comma-separated value file with a .csv extension. In this format, each column represents a particular field and every row consists of one or more fields. CSV is extensively used to import and export large data sets because of its better integration with existing applications and simplicity.

The section below shows how to convert the data of a CSV file to a JSON (JavaScript Object Notation) file without utilizing any third-party package. The key difference is that in normal conversion the values of any row can be Comma Separated and since we know, different columns are also comma-separated.

This approach will first input the contents of the CSV file in an array and then divides the content of that array based on a delimiter. All the rows of this CSV will then be changed to JSON objects which will be attached to the resultant array and will next be changed to JSON, which then produces a corresponding JSON output file.

Note: You need to consider one thing before proceeding, to implement these solutions you need to know your source file size. If you are possessing a huge CSV file with a lot of records i.e. rows, apparently you will have to use the solution with streams. Otherwise, you can go with the “standard” one, reading the entire file and when everything is read, then parsing the records.

Convert CSV to JSON file having Comma Separated values in Node.js

Method Used:

The steps below show how to complete conversion of CSV to JSON file having Comma Separated values in Node.js:

1) Read the CSV file with the help of the default fs npm package.

2) Convert the data to String and divide it into an array.

3) Produce a header array.

4) For all the remaining n-1 rows perform the following:

  • Build an empty object to add values of the current row on it.
  • Declare string str as the current array value to adjust the delimiter and save the generated string in a new string s.
  • If encountered opening quote (“) keep commas as it is, otherwise replace them with a pipe “|”.
  • Continue adding the characters we traverse to a String s.
  • Divide the string utilizing pipe delimiter | and save the values in a properties array.
  • For every header, if the value includes multiple comma-separated data, then we save it in the form of an array otherwise directly the value is saved.
  • Add the created object to our result array.

Finally, convert the resultant array to JSON and produce the JSON output file.

A Code Example to Convert CSV to JSON with “csvtojson” package: 

To convert the CSV file to JSON, you need to create a new JavaScript file in the root directory, named app.js:

$ touch app.js

You can generate the file manually if the touch command isn’t available. Just open the app.js file in your editor and add the following code:

app.js

// require CSV to JSON module

const CSVToJSON = require('csvtojson');

// convert users.csv file to JSON array

CSVToJSON().fromFile('users.csv')

    .then(users => {

        // users is a JSON array

        // log the JSON array

        console.log(users);

    }).catch(err => {

        // log error if any

        console.log(err);

    });

The above code is an example that sets a CSV file and changes it into a JSON array. If csvtojson fails to convert the CSV file to JSON, it will show an error that will be taken by the catch() method.

Write JSON to File

If you wish to store this newly created JSON array to a file for further processing. Below is an example that shows how to write the JSON array to a file:

// Write JSON array to a file

fs.writeFile('users.json', JSON.stringify(users, null, 4), (err) => {

    if (err) {

        throw err;

    }

    console.log("JSON array is saved.");

});

 

Conclusion:

That’s all for converting a Comma-Separated Values (CSV) file into a JSON array in a Node.js application. We have also discussed here how to write the JSON array to a file for additional processing. The “csvtojson” package is a powerful library to help convert CSV to JSON in Node.js and other browsers. It can also be used as a command-line tool to instantly convert a CSV file to a JSON file.

Leave a Reply

Your email address will not be published.