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

Siddharth Murugan
3 min readJul 18, 2021

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,

https://www.kaggle.com/patrickb1912/ipl-complete-dataset-20082020

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

npm init -y

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,

const fs = require('fs')

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

//Storing the entire CSV file as a string in a variablelet 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,

//As the file contains double quotes already, we are trying to remove themjsonFile = jsonFile.replace(/"/g,'');

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

//Declaring an empty array to store valueslet myDataSet = [];

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

//spliting each line with /n and /r and storing them in a variableconst 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,

//Getting the header of CSV file tableconst 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,

for (i=1;i<lines.length;i++)

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,

node csvToJson.js

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

Part of data which is converted into JSON format

Happy coding ;-)

--

--

Siddharth Murugan

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