Converting CSV to JSON using NodeJS

Elliot Forbes Elliot Forbes ⏰ 2 Minutes 📅 Apr 9, 2017

In this tutorial I’m going to be showing you how we can create a NodeJS script that takes in a csv file and outputs the contents of that CSV file as JSON.

In order to do this conversion we’ll be using the csvtojson node package. This package will do pretty much all the heavy lifting for us and having used it in production environments I can say that it’s remarkably easy to implement into a project.

Setting Up Your Project

Create a new project directory on your machine and open up this location in your terminal. Next we’ll want to install the csvtojson node module, you can do that by typing the following:

npm install csvtojson --save

This should create a node_modules directory within your project which will contain all the dependencies that csvtojson requires.

Implementation

Now that we’ve got everything set up we can begin implementation. Open up your project in your prefered text editor and create a new js file. Within this file add the following code.

//require the csvtojson converter class
var Converter = require("csvtojson").Converter;
// create a new converter object
var converter = new Converter({});

// call the fromFile function which takes in the path to your
// csv file as well as a callback function
converter.fromFile("./path-to-your-file.csv", function(err, result) {
  // if an error has occured then handle it
  if (err) {
    console.log("An Error Has Occured");
    console.log(err);
  }
  // create a variable called json and store
  // the result of the conversion
  var json = result;

  // log our json to verify it has worked
  console.log(json);
});