Bird
Raised Fist0
Fluttermobile~20 mins

JSON parsing (jsonDecode) in Flutter - Practice Problems & Coding Challenges

Choose your learning style10 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.

Practice

(1/5)
1. What does the jsonDecode function do in Flutter?
easy
A. It converts a JSON string into Dart objects like Map or List.
B. It converts Dart objects into a JSON string.
C. It formats JSON strings for better readability.
D. It sends JSON data to a web server.

Solution

  1. Step 1: Understand jsonDecode purpose

    The jsonDecode function takes a JSON formatted string and parses it into Dart objects such as Map or List.
  2. Step 2: Differentiate from encoding

    Converting Dart objects back to JSON string is done by jsonEncode, not jsonDecode.
  3. Final Answer:

    It converts a JSON string into Dart objects like Map or List. -> Option A
  4. Quick Check:

    jsonDecode = JSON string to Dart objects [OK]
Hint: jsonDecode parses JSON text into Dart objects [OK]
Common Mistakes:
  • Confusing jsonDecode with jsonEncode
  • Thinking jsonDecode formats JSON for display
  • Assuming jsonDecode sends data over network
2. Which import is required before using jsonDecode in Flutter?
easy
A. import 'dart:io';
B. import 'dart:convert';
C. import 'package:flutter/material.dart';
D. import 'dart:async';

Solution

  1. Step 1: Identify the library for JSON functions

    The jsonDecode function is part of the dart:convert library.
  2. Step 2: Confirm correct import statement

    To use jsonDecode, you must import dart:convert with import 'dart:convert';.
  3. Final Answer:

    import 'dart:convert'; -> Option B
  4. Quick Check:

    dart:convert import needed for jsonDecode [OK]
Hint: Always import dart:convert for jsonDecode [OK]
Common Mistakes:
  • Forgetting to import dart:convert
  • Importing dart:io instead
  • Assuming Flutter imports include jsonDecode
3. What is the output of this Flutter code?
import 'dart:convert';
void main() {
  String jsonString = '{"name":"Anna","age":25}';
  var user = jsonDecode(jsonString);
  print(user['age']);
}
medium
A. Anna
B. SyntaxError
C. 25
D. null

Solution

  1. Step 1: Decode JSON string to Map

    The jsonDecode converts the JSON string into a Map with keys 'name' and 'age'.
  2. Step 2: Access the 'age' key value

    Accessing user['age'] returns the integer 25.
  3. Final Answer:

    25 -> Option C
  4. Quick Check:

    jsonDecode output is Map, user['age'] = 25 [OK]
Hint: jsonDecode returns Map; access keys like a dictionary [OK]
Common Mistakes:
  • Trying to access JSON string directly without decoding
  • Expecting a string instead of integer for 'age'
  • Using wrong key name in the Map
4. What is wrong with this Flutter code snippet?
import 'dart:convert';
void main() {
  String jsonString = '{name:"John", age:30}';
  var data = jsonDecode(jsonString);
  print(data['name']);
}
medium
A. print statement syntax is incorrect
B. Missing import for jsonDecode
C. jsonDecode cannot parse strings with numbers
D. JSON string keys must be in double quotes

Solution

  1. Step 1: Check JSON string format

    JSON keys and string values must be enclosed in double quotes. Here, keys name and age are not quoted properly.
  2. Step 2: Identify parsing error cause

    Because keys lack double quotes, jsonDecode will throw a FormatException.
  3. Final Answer:

    JSON string keys must be in double quotes -> Option D
  4. Quick Check:

    JSON keys need double quotes for valid parsing [OK]
Hint: JSON keys must be double-quoted strings [OK]
Common Mistakes:
  • Using single quotes for JSON keys
  • Forgetting quotes around string values
  • Assuming jsonDecode accepts invalid JSON
5. Given this JSON string representing a list of users:
String jsonString = '[{"id":1,"name":"Alice"},{"id":2,"name":"Bob"}]';
How do you parse it to get the name of the second user in Flutter?
hard
A. var users = jsonDecode(jsonString); print(users[1]['name']);
B. var users = jsonDecode(jsonString); print(users['1']['name']);
C. var users = jsonDecode(jsonString); print(users[2]['name']);
D. var users = jsonDecode(jsonString); print(users[1].name);

Solution

  1. Step 1: Decode JSON string to List of Maps

    The JSON string represents a list with two user objects. jsonDecode converts it to a List of Maps.
  2. Step 2: Access second user and their name

    List indices start at 0, so the second user is at index 1. Accessing users[1]['name'] gives 'Bob'.
  3. Final Answer:

    var users = jsonDecode(jsonString); print(users[1]['name']); -> Option A
  4. Quick Check:

    List index 1, then key 'name' = 'Bob' [OK]
Hint: List index starts at 0; use users[1]['name'] for second item [OK]
Common Mistakes:
  • Using string '1' as index instead of integer 1
  • Accessing index 2 which is out of range
  • Trying to access Map keys with dot notation
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard