JSON APIs
Validate response shapes before trusting them.
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.
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
EasyDefault to an empty array
Read data.items and fall back to an empty array.
JAVASCRIPT · starter
const items = data.itemsCheck 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 inFinished reading?
Lock in what you just learned.
0%
Mark this lesson complete to save your place and move one step closer to the certificate.