Convert IPL CSV file data into JSON file with Node.js

When I started coding in Javascript, I mostly preferred JSON file format rather than any other file format. I thought of using IPL data and do some analysis with it. But the data which I got was in CSV format. Though we have csv-parser npm package, I thought of writing my own script which could convert the CSV file into JSON file. Let’s learn about it…

Before getting started, you need to install npm and node js in your machine. Also you may need CSV data set to convert into JSON format. I downloaded IPL ball by ball data set from below link,

First lets create a folder and initialize the npm package with below command,

Once you initialize, you need to open a new javascript and start coding from visual studio or any other IDE whichever you prefer.

Let’s have that CSV file inside sub-folder of the project and rename it as per your comfort,

Kept file inside data folder

Lets import file system package which is necessary to do file operations using below command,

I am going to store the entire CSV data into a variable using readfilesync function of file system,

let jsonFile = fs.readFileSync("data/iplBallData.csv").toString()

Make sure that you add .toString() to convert entire dataset into string.

As the CSV file contains double quotes, we will try to remove those with .replace function,

jsonFile = jsonFile.replace(/"/g,'');

Its time to declare an empty array to store the entire data set,

let myDataSet = [];

Now, we need to store each record of the file into a variable by using split function,

const lines = jsonFile.split('\r\n')

\r and \n has the same property of making new file.

It’s time to get the header of the table and store it in a separate variable,

const heading = lines[0].split(',')

Lines[0] will have the first line of the record, which has the table header

Let’s make a loop to iterate through each record,

We may need one more loop to assign each table header with a value like a JSON format. With both loop our code will look like below,

In above piece of code, we have an empty object — singleData which will hold single record. Once it has a entire record into it, it will be pushed to an empty array named myDataSet.

We have the entire data into myDataSet, let’s now store it into a JSON file with file system function as below,

Alright, our complete code will look like below,

Try to run this code with node command as below,

That’s it!! You are done! You will get the output as below,

Part of data which is converted into JSON format

Happy coding ;-)



Programmer who loves to do things creatively. #automationTester by profession #javascript #nodejs #reactjs

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Siddharth Murugan

Programmer who loves to do things creatively. #automationTester by profession #javascript #nodejs #reactjs