Thursday, July 9, 2020

Reading and Writing the data to CSV files with Node.js

Reading and Writing the data to CSV files with Node.js


CSV Data


A CSV file stands for a comma-separated values file. This is a plain text file containing the data written in a particular format described for CSV files. All the fields in a CSV file are separated using commas in a particular line. We can store CSV data in the tabular form very easily. RFC4180 is the most commonly used CSV format.

For example,

NAME,AGE,GRADE,MARKS
1,ALEX,16,A,88
2,STEVE,16,C,34
3,JHON,17,B,66
4,WILEY,16,B,75
5,SMITH,18,A,82
6,DAVE,16,A,90
7,KYLE,17,C,44
8,SAM,18,A,85
9,MAX,16,B,77
10,RON,17,A,93


Reading CSV Files in Node.js


The Node.js fs module can be used to read CSV data. There are many modules available for performing various operations on CSV data in Node.js such as ‘csv-parser’. We can perform parsing operations with this module very easily. 

We can install the csv-parser module using the following command,

npm install csv-parser

For example,

const csv = require('csv-parser');
const fs = require('fs');

fs.createReadStream('record.csv')
  .pipe(csv())
  .on('data', (line) => {
    console.log(line);
  })
  .on('end', () => {
    console.log('CSV data displayed successfully');
  });


The data from readStream is piped into a csv object. The data event is fired every time a new row is processed from the entire CSV data. When entire data from the CSV file is consumed the ‘end’ event is fired.

The output will appear as,

{ '': '1', NAME: 'ALEX', AGE: '16', GRADE: 'A', MARKS: '88' }
{ '': '2', NAME: 'STEVE', AGE: '16', GRADE: 'C', MARKS: '34' }
{ '': '3', NAME: 'JHON', AGE: '17', GRADE: 'B', MARKS: '66' }
{ '': '4', NAME: 'WILEY', AGE: '16', GRADE: 'B', MARKS: '75' }
{ '': '5', NAME: 'SMITH', AGE: '18', GRADE: 'A', MARKS: '82' }
{ '': '6', NAME: 'DAVE', AGE: '16', GRADE: 'A', MARKS: '90' }
{ '': '7', NAME: 'KYLE', AGE: '17', GRADE: 'C', MARKS: '44' }
{ '': '8', NAME: 'SAM', AGE: '18', GRADE: 'A', MARKS: '85' }
{ '': '9', NAME: 'MAX', AGE: '16', GRADE: 'B', MARKS: '77' }
{ '': '10', NAME: 'RON', AGE: '17', GRADE: 'A', MARKS: '93' }


Writing data to CSV file


We can write data to file using the fs module because the CSV data is a kind of plain text in itself. However, we can perform this task easily using the 'csv-writer' module. 

To install this module use the following command,

npm install csv-writer

We can write a simple code to write data to CSV file as follows,

const createCsvWriter = require('csv-writer').createObjectCsvWriter;
const csvWriter = createCsvWriter({
  path: 'record.csv',
  header: [
    {id: 'StudentName', title: 'StudentName'},
    {id: 'Roll', title: 'Roll'},
    {id: 'Age', title: 'Age'},
    {id: 'Dept', title: 'Dept'},
  ]
});

const record = [
  {
    StudentName: 'Alex',
    Roll: 15,
    Age: 18,
    Dept: 'ME'
  }, {
    StudentName: 'Smith',
    Roll: 55,
    Age: 19,
    Dept: 'CSE',
  }, {
    StudentName: 'Rob',
    Roll: 48,
    Age: 18,
    Dept: 'ECE'
  }
];

csvWriter
  .writeRecords(record)
  .then(()=> console.log('Data written successfully'));

After compiling the above code using the node command, a new file with a name record.csv will be created and the following data will be written in this file.

StudentName,Roll,Age,Dept
Alex,15,18,ME
Smith,55,19,CSE
Rob,48,18,ECE

Reading CSV data and converting to JSON format


We can use the 'csvtojson' module to read a CSV file and convert the data into the JSON format. We can install 'csvtojson' module using the npm install command.

const csv=require('csvtojson');
const converter=csv()
.fromFile('./record.csv')
.then((json)=>{
    console.log(json);
});

The results will display the JSON data stored in the record file.