0
0
React Nativemobile~20 mins

JSON response parsing in React Native - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
JSON Parsing Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
ui_behavior
intermediate
2:00remaining
Displaying parsed JSON data in a list
You receive this JSON response from a server:
{"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?
A
const data = JSON.stringify(jsonResponse);
<FlatList data={data.users} keyExtractor={item => item.id.toString()} renderItem={({item}) => <Text>{item.name}</Text>} />
B
const data = JSON.parse(jsonResponse.users);
<FlatList data={data} keyExtractor={item => item.id.toString()} renderItem={({item}) => <Text>{item.name}</Text>} />
C
const data = JSON.parse(jsonResponse);
<FlatList data={data.users} keyExtractor={item => item.id.toString()} renderItem={({item}) => <Text>{item.name}</Text>} />
D
const data = JSON.parse(jsonResponse);
<FlatList data={data} keyExtractor={item => item.id.toString()} renderItem={({item}) => <Text>{item.name}</Text>} />
Attempts:
2 left
💡 Hint
Remember to use JSON.parse to convert the JSON string into an object before accessing its properties.
📝 Syntax
intermediate
1: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"}]}';
A
const data = JSON.parse(response.posts);
const title = data[0].title;
B
const data = JSON.stringify(response);
const title = data.posts[0].title;
C
const data = JSON.parse(response);
const title = data[0].posts.title;
D
const data = JSON.parse(response);
const title = data.posts[0].title;
Attempts:
2 left
💡 Hint
Use JSON.parse on the whole JSON string before accessing nested properties.
🔧 Debug
advanced
2:00remaining
Fixing runtime error when parsing JSON
You have this code snippet:
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?
AThe variable <code>response</code> is not a string but already an object, so parsing it again causes error.
BThe code is correct and should not throw an error; the error must be from elsewhere.
CThe JSON string is correct but the property <code>items</code> does not exist in the parsed object.
DThe JSON string is malformed and JSON.parse fails silently returning undefined.
Attempts:
2 left
💡 Hint
Check the type of the variable before parsing it again.
🧠 Conceptual
advanced
1: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));
Aresponse.json()
BJSON.parse(response)
Cresponse.text()
DJSON.stringify(response)
Attempts:
2 left
💡 Hint
The fetch response object has a method to parse JSON directly.
navigation
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 = ???;
Aconst items = navigation.getParam('items');
Bconst items = route.params.items;
Cconst items = props.navigation.state.params.items;
Dconst items = navigation.params.items;
Attempts:
2 left
💡 Hint
In React Navigation v5 and later, route.params contains passed parameters.