File Reading Tips

How to Read a CSV File in Node.js

And Use Classes with Object.assign() to Store The Data

Photo by Mika Baumeister on Unsplash

In a prior article I showed how easy it is to read Excel workbook data. In this article we will expand on this concept of reading data from a file. In this case a structured comma separated values file. The data will be received as an Array of json Objects.

This will allow us to explore the use of JavaScript Classes and Object.assign() to create an Array of Employee Objects, which will be much easier to manipulate than the json Objects themselves.

Reading The File

Setup

You will of need to have node.js installed.

Create the csv file below and name it Data.csv.

Comma Separated Data

I will be using Visual Studio Code (VSCode).

Create package.json with index.js as the default file by opening your Terminal window and typing,

npm init -y

2. To install the npm package we need, in your Terminal window, type,

npm i csvtojson

3. Create a file named index.js and open it in the editor.

Reading The CSV File and Getting an Array of Json Objects

Add the following code to your index.js file.

Invoking csv() returns a promise.

const csv=require('csvtojson') // Invoking csv returns a promise

const converter=csv()

.fromFile('./Data.csv')

.then((json)=>{

console.log(json);

})

Use CTRL+F5 to run without debugging. You should see an array of well structured json Objects. You could also use console.table().

Array of json Objects

Working With The Data

I would like the data in a data structure I find easier to work with. A JavaScript Class. Since the data is an Array of json Objects, we will create a Class called Employee. Assign each each json Object, using Object.assign(), to an Employee Object and store these in an Array of Employee Objects named emp[].

This way, we can use the getter methods of the Employee Class to get whatever data we wish to work with.

Creating the Employee Class

At the top of index.js, add the following Class and Array.

class Employee{

set Name(Name){

this._Name=Name;

}

set Title(Title){

this._Title=Title;

}

set Salary(Salary){

this._Salary=Salary;

}

get Name(){

return this._Name;

}

get Title(){

return this._Title;

}

get Salary(){

return this._Salary;

}

constructor(){

} }

let emp=[];// Array to store Employee Objects

We will create new Employee Objects and Push them on to the Array in the first .then() handler of the promise.

We will chain this with another .then() to display the Names of each Employee in the Array of Employees by invoking the Name getter method.

However, we could access whatever data we wanted to at this point.

Modify the index.js invocation of csv() as follows and run using CTRL+F5.

// Invoking csv returns a promise

const converter=csv()

.fromFile('./Data.csv')

.then((json)=>{

let e;// Will be an Employee Object

json.forEach((row)=>{

e=new Employee();// New Employee Object

Object.assign(e,row);// Assign json to the new Employee

emp.push(e);// Add the Employee to the Array



}); }).then(()=>{

// Output the names of the Employees

emp.forEach((em)=>{

console.log(em.Name);// Invoke the Name getter

});

});

Outputting the Names from the Array of Employees

The Complete code is below.

class Employee{

set Name(Name){

this._Name=Name;

}

set Title(Title){

this._Title=Title;

}

set Salary(Salary){

this._Salary=Salary;

}

get Name(){

return this._Name;

}

get Title(){

return this._Title;

}

get Salary(){

return this._Salary;

}

constructor(){

} }

let emp=[];// Array to store Employee Objects const csv=require('csvtojson')

// Invoking csv returns a promise

const converter=csv()

.fromFile('./Data.csv')

.then((json)=>{

let e;

json.forEach((row)=>{

e=new Employee();// New Employee Object

Object.assign(e,row);// Assign json to the new Employee

emp.push(e);// Add the Employee to the Array



}); }).then(()=>{

// Output the names of the Employees

emp.forEach((em)=>{

console.log(em.Name);// Invoke the Name getter

});

});

Conclusion

There are actually quite a few npm packages for processing files. I have chosen this one as I wanted to get json data. This in turn allowed us to explore the use of Classes and Object.assign().

More information and options on the package csvtojson can be found below.

More information on Classes and Object.assign() can be found below as well as in the articles that follow.

Thank you for reading and happy coding!

You may also enjoy,

below is part one on JavaScript Classes. It is in a later part that setters and getters are used. Links exist in this article to all four parts on Classes. I encourage you to read them. Classes are quite a useful data structure.