Challenge - 5 Problems
JSON Parsing Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ ui_behavior
intermediate2:00remaining
What is the output of this Flutter widget after JSON parsing?
Given this Flutter code snippet that parses JSON and displays a name, what will the Text widget show?
Flutter
import 'dart:convert'; import 'package:flutter/material.dart'; void main() => runApp(const MyApp()); class MyApp extends StatelessWidget { const MyApp({super.key}); @override Widget build(BuildContext context) { const jsonString = '{"name": "Alice", "age": 30}'; final Map<String, dynamic> user = jsonDecode(jsonString); return MaterialApp( home: Scaffold( body: Center( child: Text(user['name']), ), ), ); } }
Attempts:
2 left
💡 Hint
Look at how jsonDecode converts the JSON string into a Dart Map and how the 'name' key is accessed.
✗ Incorrect
jsonDecode converts the JSON string into a Map. Accessing user['name'] returns the string 'Alice', which is displayed by the Text widget.
📝 Syntax
intermediate1:30remaining
Which option correctly parses a JSON string into a Dart Map?
Choose the correct Dart code snippet that parses the JSON string '{"key": "value"}' into a Map.
Attempts:
2 left
💡 Hint
JSON keys and string values must be in double quotes.
✗ Incorrect
Option D uses valid JSON syntax with double quotes around keys and string values, and correctly assigns the result to a Map.
🔧 Debug
advanced1:30remaining
What error does this code raise when parsing invalid JSON?
Consider this Dart code:
final jsonString = '{"name": "Bob", age: 25}';
final user = jsonDecode(jsonString);
What error will occur?
Flutter
final jsonString = '{"name": "Bob", age: 25}'; final user = jsonDecode(jsonString);
Attempts:
2 left
💡 Hint
Check JSON syntax: keys must be in double quotes.
✗ Incorrect
The JSON string is invalid because the key 'age' is not in double quotes. jsonDecode throws a FormatException indicating unexpected character.
🧠 Conceptual
advanced1:30remaining
What type does jsonDecode return when parsing a JSON array?
If you parse the JSON string '[1, 2, 3]' using jsonDecode in Dart, what is the runtime type of the returned object?
Flutter
final jsonString = '[1, 2, 3]'; final result = jsonDecode(jsonString);
Attempts:
2 left
💡 Hint
Think about how JSON arrays map to Dart types.
✗ Incorrect
jsonDecode returns a List when parsing a JSON array, because JSON arrays correspond to Dart lists.
❓ lifecycle
expert2:00remaining
When should you call jsonDecode in a Flutter app to parse API response data?
You fetch JSON data from a web API in a Flutter app. To parse the JSON string into Dart objects, when is the best time to call jsonDecode?
Attempts:
2 left
💡 Hint
Consider when you have the raw JSON string and when you need Dart objects.
✗ Incorrect
You should parse the JSON string right after receiving it, so you can update your app state and UI with Dart objects. Parsing inside build() is inefficient and can cause errors.