Challenge - 5 Problems
JSON Parsing Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ ui_behavior
intermediate2:00remaining
Displaying parsed JSON data in a list
You receive this JSON response from a server:
Which React Native code snippet correctly parses this JSON and displays the user names in a
{"users": [{"id": 1, "name": "Alice"}, {"id": 2, "name": "Bob"}]}Which React Native code snippet correctly parses this JSON and displays the user names in a
FlatList?React Native
const jsonResponse = '{"users": [{"id": 1, "name": "Alice"}, {"id": 2, "name": "Bob"}]}'; // Which snippet correctly parses and displays the names?
Attempts:
2 left
💡 Hint
Remember to use JSON.parse to convert the JSON string into an object before accessing its properties.
✗ Incorrect
Option C correctly parses the JSON string into an object, then accesses the 'users' array to pass as data to FlatList. Option C wrongly uses JSON.stringify which converts an object to a string. Option C passes the whole parsed object instead of the users array. Option C tries to parse jsonResponse.users which is undefined because jsonResponse is a string.
📝 Syntax
intermediate1:30remaining
Correct JSON parsing syntax
Which option correctly parses a JSON string stored in variable
response and accesses the title property of the first item in the posts array?React Native
const response = '{"posts": [{"id": 1, "title": "Hello"}, {"id": 2, "title": "World"}]}';Attempts:
2 left
💡 Hint
Use JSON.parse on the whole JSON string before accessing nested properties.
✗ Incorrect
Option D correctly parses the JSON string and accesses the first post's title. Option D uses JSON.stringify which converts an object to a string, so data.posts is undefined. Option D tries to parse response.posts which is undefined because response is a string. Option D tries to access data[0] which is undefined because data is an object with a posts array.
🔧 Debug
advanced2:00remaining
Fixing runtime error when parsing JSON
You have this code snippet:
But it throws an error:
const response = '{"items": [{"id": 1, "value": 10}, {"id": 2, "value": 20}]}';
const data = JSON.parse(response);
console.log(data.items.length);But it throws an error:
TypeError: Cannot read property 'length' of undefined. What is the most likely cause?Attempts:
2 left
💡 Hint
Check the type of the variable before parsing it again.
✗ Incorrect
If response is already an object, calling JSON.parse on it throws an error or returns undefined. This causes data.items to be undefined, so accessing length fails. The JSON string shown is correct, so A and C are unlikely. B is incorrect because the error message points to this code.
🧠 Conceptual
advanced1:30remaining
Understanding asynchronous JSON fetching
In React Native, you fetch JSON data from a server using
fetch. Which code snippet correctly parses the JSON response and updates state with the data?React Native
fetch('https://example.com/data')
.then(response => ???)
.then(data => setData(data))
.catch(error => console.error(error));Attempts:
2 left
💡 Hint
The fetch response object has a method to parse JSON directly.
✗ Incorrect
The fetch API response object has a .json() method that returns a promise resolving to the parsed JSON. JSON.parse expects a string, but response is a Response object, so B is wrong. A returns raw text, not parsed JSON. D converts an object to a string, not parsing.
expert
2:30remaining
Passing parsed JSON data between screens
You parsed JSON data in Screen A and want to pass the array
items to Screen B using React Navigation. Which code snippet correctly passes and accesses the data?React Native
// In Screen A:
navigation.navigate('ScreenB', { items: parsedData.items });
// In Screen B:
const items = ???;Attempts:
2 left
💡 Hint
In React Navigation v5 and later, route.params contains passed parameters.
✗ Incorrect
Option B uses route.params which is the correct way to access passed parameters in functional components with React Navigation v5+. Option B is deprecated from older versions. Option B uses props.navigation.state.params which is outdated and may not work. Option B is invalid syntax.