Parse a CSV file into JSON

Taylor Bell
InstructorTaylor Bell
Share this video with your friends

Social Share Links

Send Tweet
Published 5 years ago
Updated 3 years ago

The humble CSV file is a common way to store tabular data. Some people love using software like Excel or Google Sheets to manipulate CSV files, but I prefer to use JavaScript.

In this lesson, we will read a CSV file and parse it as JSON.

Instructor: [0:00] This is a data set of the World Cup 2018 matches. You can see that we have the round number, date, location, home and away team, the group, and the result. I want to do some stuff with this data but I'm not really good at spreadsheet stuff, so I'm going to do it in JavaScript.

[0:15] I've created a project directory and inside of it I have my World Cup CSV. To start the project, I'm going to do yarn init, I'm just going to hit enter a bunch of times, because I'm fine with it. I could have passed the -y flag, but you know.

[0:28] One of the packages I like to use is read-csv-json. I'm going to do yarn add read-csv-json, once this is installed, I'm going to open VS Code to this current directory. Before we get started, let's take a look at the npm page for read-csv-json. Looks like the first thing that we do is create a variable called csv_module where we bring the package in, point it at the file, give it an array of strings that represent the columns, and then invoke a new instance of it.

[0:58] Then from here, we have a promise, which we'll do with async/await instead, but we'll get there in a minute.

[1:05] Just like we saw in the example, the first thing we'll do is create a new csv_module variable, and we'll do this by doing const csv_module = require (read-csv-json) in Kebab case, and then we'll create a new file name variable. We use a ./ because it will be looking for the World Cup CSV in the same directory that we're currently in, so we do ./world_cup_2018.csv, and then we need an array of field names which will be the strings that get read from our CSV file.

[1:44] We'll hop over to the CSV, we'll copy and paste the first line on the CSV file into the brackets, and then we'll go through and add quotes around each one. Now that we've created the field names, we will do a const csv_read, so this is the thing that's going to return the promise. This will be a new csv_module, and we pass it the file name and we pass it the field names.

[2:09] When it comes to asynchronous operations, I try to use async/await. In order to do that we need to create an async function that will call read_file, then inside of here we'll do a new variable called result that will be what we get back from csv_module. We'll do await csv_module.get-csv-json. After it reads the file we'll just console.log result. Now if we come down and run node index.js, nothing happens because we're not actually calling read file.

[2:42] How about now? We have an error, csv_module.get-csv-json is not a function. Oh, right, because this is csv_read this is our variable not the actual one. Programming. There we go. Now when we run index.js, we see a JSON representation of our CSV file.

egghead
egghead
~ 3 minutes ago

Member comments are a way for members to communicate, interact, and ask questions about a lesson.

The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io

Be on-Topic

Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.

Avoid meta-discussion

  • This was great!
  • This was horrible!
  • I didn't like this because it didn't match my skill level.
  • +1 It will likely be deleted as spam.

Code Problems?

Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context

Details and Context

Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!

Markdown supported.
Become a member to join the discussionEnroll Today