0
0
Fluttermobile~20 mins

JSON parsing (jsonDecode) in Flutter - 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
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']),
        ),
      ),
    );
  }
}
AThe app shows: Alice
BThe app shows: {name: Alice, age: 30}
CThe app shows: null
DThe app crashes with a runtime error
Attempts:
2 left
💡 Hint
Look at how jsonDecode converts the JSON string into a Dart Map and how the 'name' key is accessed.
📝 Syntax
intermediate
1: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.
Afinal Map data = jsonDecode('{key: "value"}');
Bfinal Map<String, String> data = jsonDecode('{key: value}');
Cfinal data = jsonDecode('{"key": value}');
Dfinal Map<String, dynamic> data = jsonDecode('{"key": "value"}');
Attempts:
2 left
💡 Hint
JSON keys and string values must be in double quotes.
🔧 Debug
advanced
1: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);
ATypeError: Cannot assign int to String
BNo error, user is parsed correctly
CFormatException: Unexpected character at position 16
DNullPointerException at jsonDecode
Attempts:
2 left
💡 Hint
Check JSON syntax: keys must be in double quotes.
🧠 Conceptual
advanced
1: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);
AMap<String, dynamic>
BList<dynamic>
CString
Dint
Attempts:
2 left
💡 Hint
Think about how JSON arrays map to Dart types.
lifecycle
expert
2: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?
AImmediately after receiving the response string, before updating any UI
BAfter the app is closed and restarted
COnly when the user taps a button to refresh the UI
DInside the build() method of a widget
Attempts:
2 left
💡 Hint
Consider when you have the raw JSON string and when you need Dart objects.