Node.js Tutorial: Parsing CSV

Kavit (zenwraight)
3 min readMay 11, 2022

For folks who want to have a thorough walk through can watch the video linked below:-

Introduction

CSVs, or comma-separated values, are a standard and fundamental format for storing and transmitting tabular datasets in statistical analysis tools and spreadsheet programs. It is usual for governmental bodies and other key organizations to distribute official datasets in CSV format due to its popularity.
CSV files are written in one application and often display improperly in another, despite their simplicity, popularity, and broad use. Because there is no formal universal CSV specification to which apps must conform, this is the case. As a result, there are various implementations with minor differences.
CSV files can be read, written, and parsed in almost all modern operating systems and programming languages, including JavaScript and the Node.js runtime environment.

In this article, we will learn how to manage CSV files in Node. The package we will look at is csv-parse .

What is a CSV file?

CSV files are ordinary text files comprised of data arranged in rectangular form. When you save a tabular data set in CSV format, a new line character will separate successive rows while a comma will separate consecutive entries in a row. The image below shows a tabular data set and its corresponding CSV format.

In the above CSV data, the first row is made up of field names, though that may not always be the case. Therefore, it is necessary to investigate your data set before you start to read and parse it.

Although the name Comma Separated Values seems to suggest that a comma should always separate subsequent entries in each record, some applications generate CSV files that use a semicolon as a delimiter instead of a comma.

Implementation

Let’s create a node application using npm init .

Now, we need to install few dependencies, such as express for our node server and csv which comes with a bunch of functionality for parsing csv files.

npm install express csv

After that we will be writing a small code inside our server.js file. The code is provided below:-

Most of the code is self-explanatory but I will go over a few important details.

We use createReadStream method in the code. Using createReadStream function of the fs module, you can use streams instead to reduce memory footprint and data processing time.

After that we call a parser() method which basically uses inbuilt parse() method that comes with the third party package csv .

Conclusion

The Comma Separated Values (CSV) format is one of the most widely used data interchange formats. CSV datasets are basic text files that can be read by humans and machines. There is no global standard, despite its popularity.

Follow me for updates like this.

Connect with me on:-

Twitter 👦🏻:- https://twitter.com/kmmtmm92

Youtube 📹:- https://www.youtube.com/channel/UCV-_hzlbVSlobkekurpLOZw/about

Github 💭:- https://github.com/Kavit900

Instagram 📸:- https://www.instagram.com/code_with_kavit/

--

--