AJAX & Async Requests·AJAX from first request to live updates

JSON APIs

Validate response shapes before trusting them.

Beginner 12 min read Lesson 4 of 300 Challenge included
What you will be able to do
  • Check the top-level shape.
  • Default missing collections to empty arrays.
  • Do not trust server messages as HTML.
  • Log the response when debugging.

In plain English

A 200 response is not a contract. Validate the fields you use, handle missing arrays and avoid assuming every object has every key. A small runtime check prevents blank pages.

shape.jsJavaScript
const data = await response.json()
const lessons = Array.isArray(data.lessons) ? data.lessons : []
const first = lessons[0] ?? { title: 'No lessons yet' }
console.log(first.title)
Your turn

Default to an empty array

Easy

Read data.items and fall back to an empty array.

JAVASCRIPT · starter
const items = data.items
Check your understanding before moving on.
Knowledge checkQuestion 1

Why validate a JSON response shape?

Select an answer to continue
Study tools

Make it yours.

Keep a private note for this lesson, then turn the key ideas into flashcards you can revisit.

Sign in to save notes and flashcards.Both are private to your account and stay with your progress.
Sign in
Finished reading?

Lock in what you just learned.

Mark this lesson complete to save your place and move one step closer to the certificate.

Next: Forms without a reload
Lesson 4 of 300 · 0% of course