Lesson 1 of 8
Reading and writing JSON
- 11 min
- 4 checks
- 3 hints
- Go to task
Goal
Turn text into objects
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.
Your task
Parse raw into objects and show them in the list, and handle the invalid text without crashing the page.
Checks
4 checks- Two items from the text are shown
- The names and prices are visible
- The page kept running after the invalid text
- The error message is shown