Lesson 1 of 8

Reading and writing JSON

Goal

Turn text into objects

01

Explanation

What the server sends you is text, not an object. You use JSON.parse to turn it into objects and arrays you can work with, and JSON.stringify to go the other way when you send data.

JSON's rules are stricter than JavaScript's: it only accepts double quotes, it doesn't allow a comma after the last item, and it doesn't allow comments. Most "Unexpected token" errors come from breaking one of these three rules.

If the text isn't valid, JSON.parse throws an error, so wrap it in try/catch whenever the text comes from an outside source.

02

Your task

Parse raw into objects and show them in the list, and handle the invalid text without crashing the page.

Checks

4 checks
  1. Two items from the text are shown
  2. The names and prices are visible
  3. The page kept running after the invalid text
  4. The error message is shown
03

Hints

0/3
Track lessons0 of 8 done

Learn to read JSON, wait for the server to respond, and handle loading and failure states. That's what separates a demo from a product people can rely on.

Intermediate level, 8 lessons, 2 hours in total.

index.html
Preview
index.html100%
–/4

Press “Run and check” to grade your code.

4 checks listed under “Your task”.

Track lessons0 of 8 done

Learn to read JSON, wait for the server to respond, and handle loading and failure states. That's what separates a demo from a product people can rely on.

Intermediate level, 8 lessons, 2 hours in total.