Fetch JSON Parsing
Predict Output Medium
Question
What does the data variable contain?
Code
// Response body: {"users": ["Alice", "Bob"]}
fetch("/api/users")
.then(res => res.json())
.then(data => console.log(data.users[0]));
Hint
res.json() parses the body, then data is the object.
Explanation
fetch returns a Response. res.json() parses the body as JSON, returning a promise that resolves to {users: ["Alice", "Bob"]}. Then data.users is ["Alice", "Bob"], and data.users[0] is "Alice". The fetch chain uses two .then() calls: one for parsing, one for using the data.
Related Resources
Related Lessons
- Fetch API
Learn the Fetch API.
Look up unfamiliar terms
A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.
Decode error messages
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.
Build real projects
Apply what you learned by building guided projects with starter code and solutions.