Load a CSV File

In Load a JSON File With fetch(), Sam’s starter recipes moved into data/recipes.json, and the page loaded them with two awaits and a check of response.ok. Ayumi’s recipe list deserves the same treatment. Pasting her message into the Console worked once, but Sam wants her list as a file in the project, so the page can read it whenever it loads.

In this lesson, you’ll load that file with fetch(), read it as plain text, and turn it into recipe objects with the parsing you already know. You’ll also learn to tell one kind of loading failure from another, so an error tells you where to look. Start by saving Ayumi’s list as a file.

Read Ayumi’s File as Text

Ayumi’s list is a CSV file. CSV stands for comma-separated values: plain text with one record per line and commas between the fields. Spreadsheets export this format, and most CSV files start with a header row that names the columns. In the recipes folder, save this as data/ayumi.csv, next to recipes.json:

Language: CSV Filename: recipes/data/ayumi.csv
name,minutes,servings
Miso soup,15,2
Okonomiyaki,30,4
Matcha cookies,25,12

To try the file out without touching the recipe box, give it a small test page of its own. Save this as recipes/ayumi.html. Its list starts empty, and the script will fill it:

Language: HTML Filename: recipes/ayumi.html
<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Ayumi's list</title>
  <script type="module" src="ayumi.js"></script>
</head>
<body>
  <h1>Ayumi's list</h1>
  <ul id="ayumi"></ul>
</body>
</html>

The script uses the recipe from the last lesson, with one change. A CSV file isn’t JSON, so response.json() would throw a SyntaxError on it. Instead, response.text() reads the body and gives it back as one string, without parsing it. Save this as recipes/ayumi.js:

Language: JavaScript Filename: recipes/ayumi.js
const url = "data/ayumi.csv";
const response = await fetch(url);
if (!response.ok) {
  throw new Error(`Couldn't load ${url}: status ${response.status}`);
}
const text = await response.text();
console.log(text);

With Live Preview running, open http://127.0.0.1:3000/recipes/ayumi.html in Chrome or Edge, and open the Console with Ctrl+Shift+J, or Cmd+Option+J on macOS. The heading shows, the list is still empty, and the Console holds the file’s text exactly as you saved it:

Language: Program Output
name,minutes,servings
Miso soup,15,2
Okonomiyaki,30,4
Matcha cookies,25,12

That’s the same kind of string you pasted in by hand last time, header row aside. In the Network panel, with the Fetch/XHR filter on, a reload shows one row, ayumi.csv, with status 200. Its Response tab shows the same four lines.

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Become a Member to join the conversation.