Bird
Raised Fist0
Fluttermobile~10 mins

JSON parsing (jsonDecode) in Flutter - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to parse a JSON string into a Dart map using jsonDecode.

Flutter
import 'dart:convert';

void main() {
  String jsonString = '{"name": "Alice", "age": 30}';
  var user = [1](jsonString);
  print(user['name']);
}
Drag options to blanks, or click blank then click option'
AjsonDecode
BjsonEncode
CjsonParse
DjsonStringify
Attempts:
3 left
💡 Hint
Common Mistakes
Using jsonEncode instead of jsonDecode
Trying to parse JSON without importing dart:convert
2fill in blank
medium

Complete the code to access the 'age' value from the parsed JSON map.

Flutter
import 'dart:convert';

void main() {
  String jsonString = '{"name": "Bob", "age": 25}';
  var user = jsonDecode(jsonString);
  int age = user[1];
  print(age);
}
Drag options to blanks, or click blank then click option'
A{'age'}
B.age
C(age)
D['age']
Attempts:
3 left
💡 Hint
Common Mistakes
Using dot notation to access map keys
Using parentheses or curly braces instead of square brackets
3fill in blank
hard

Fix the error in the code to correctly parse a JSON list of strings.

Flutter
import 'dart:convert';

void main() {
  String jsonString = '["apple", "banana", "cherry"]';
  List<String> fruits = List<String>.from([1](jsonString));
  print(fruits[1]);
}
Drag options to blanks, or click blank then click option'
AjsonEncode
BjsonParse
CjsonDecode
DjsonStringify
Attempts:
3 left
💡 Hint
Common Mistakes
Using jsonEncode instead of jsonDecode
Not converting the decoded object to List
4fill in blank
hard

Fill both blanks to parse a JSON string and access the first user's email.

Flutter
import 'dart:convert';

void main() {
  String jsonString = '{"users": [{"email": "a@example.com"}, {"email": "b@example.com"}]}';
  var data = [1](jsonString);
  String email = data['users'][2]['email'];
  print(email);
}
Drag options to blanks, or click blank then click option'
AjsonDecode
BjsonEncode
C[0]
D.0
Attempts:
3 left
💡 Hint
Common Mistakes
Using dot notation to access list elements
Using jsonEncode instead of jsonDecode
5fill in blank
hard

Fill all three blanks to parse a JSON string and create a map of user names to ages for users older than 20.

Flutter
import 'dart:convert';

void main() {
  String jsonString = '{"users": [{"name": "Anna", "age": 22}, {"name": "Ben", "age": 19}]}';
  var data = jsonDecode(jsonString);
  var result = {user[1]: user[2] for user in data['users'] if user['age'] [3] 20};
  print(result);
}
Drag options to blanks, or click blank then click option'
A['name']
B['age']
C>
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using dot notation instead of square brackets for map keys
Using less than operator instead of greater than
Mixing up keys and values in the map comprehension

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