0
0
Fluttermobile~10 mins

JSON parsing (jsonDecode) in Flutter - Interactive Code Practice

Choose your learning style9 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