Join egghead, unlock knowledge.

Want more egghead?

This lesson is for members. Join us? Get access to all 3,000+ tutorials + a community with expert developers around the world.

Unlock This Lesson
1×
Become a member
to unlock all features
Autoplay

    Parse a CSV file into JSON

    Taylor BellTaylor Bell
    javascriptJavaScript

    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.

    Code

    Code

    Become a Member to view code

    You must be a Member to view code

    Access all courses and lessons, track your progress, gain confidence and expertise.

    Become a Member
    and unlock code for this lesson
    Transcript

    Transcript

    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.

    Discuss

    Discuss