Skip to content
+91 89607 57615
Code Craft — web development and digital marketing
Lesson 1126 min

Fetching data with fetch() and JSON

Load the menu from a JSON file with fetch(), async and await, and handle errors properly.

Taught by Sandesh Sahetiya

The video is part of the full course

In this lesson you will

  • Read and write JSON
  • Request a file with fetch() using async and await
  • Check response.ok and handle failures with try and catch
  • Run a local web server for development

In the last lesson the menu lived inside script.js. Here it moves to its own file, menu.json, which the page downloads. That's how most sites work: the page asks a server for data and draws it, so the menu can change without touching the code.

JSON

JSON (JavaScript Object Notation) is text that looks like JavaScript objects and arrays, with stricter rules: keys and strings use double quotes, and there are no comments or trailing commas.

menu.jsonDownload
[
  { "name": "Sourdough loaf", "description": "Slow-fermented for 24 hours.", "price": 180, "eggless": true },
  { "name": "Butter buns", "description": "Soft, golden, sold in sixes.", "price": 120, "eggless": true },
  { "name": "Carrot cake", "description": "By the slice or whole.", "price": 90, "eggless": false },
  { "name": "Masala bun", "description": "Spiced potato in a soft bun.", "price": 40, "eggless": true },
  { "name": "Multigrain loaf", "description": "Seven grains and seeds.", "price": 160, "eggless": true },
  { "name": "Chocolate brownie", "description": "Dense and fudgy.", "price": 70, "eggless": false }
]

Continue with the full course

The rest of this lesson, the narrated video, the PDF and the exercise are part of Web Development Foundations. One payment, lifetime access.

Already bought it? Sign in with the email you paid with.