๐ฆ Lesson 9: JSON & Data Models
The Data Challenge
Every app needs data. APIs send JSON. Your job? Transform that raw text into clean, type-safe Dart objects!
๐ API
Raw JSON text
โ
๐ jsonDecode
Map<String, dynamic>
โ
๐๏ธ fromJson()
Type-safe Model
โ
โจ Your App
Safe, clean data
Today's Project: "Wikipedia Explorer" Models
We'll build a complete data layer for a Wikipedia client:
- ๐ Article summary model
- ๐ Search results model
- ๐งฉ Nested JSON structures
- ๐ก๏ธ Pattern matching validation
- ๐ฆ Organized package structure
Step 1
JSON Parsing Fundamentals
๐ฌ Live JSON Parser
Paste JSON and see it decoded in real-time:
๐ JSON Input:
๐ค Parsed Result:
// Loading...
// Loading...
Step 2
Building Data Model Classes
๐๏ธ Interactive Model Builder
Design your data model and generate the Dart class:
๐ Generated Dart Class:
class User {
final int id;
final String name;
final String? email;
final int? age;
User({
required this.id,
required this.name,
this.email,
this.age,
});
factory User.fromJson(Map json) {
return User(
id: json['user_id'] as int,
name: json['full_name'] as String,
email: json['email'] as String?,
age: json['age'] as int?,
);
}
Map toJson() => {
'user_id': id,
'full_name': name,
if (email != null) 'email': email,
if (age != null) 'age': age,
};
}
๐งช Test Your Model
Enter JSON and see the model in action!
Step 3
Pattern Matching for JSON
๐ฏ Pattern Matching Workshop
See how pattern matching validates and extracts data simultaneously:
๐ด Traditional (Pre-Dart 3)
factory User.fromJson(Map<String, dynamic> json) {
return User(
id: json['id'] as int,
name: json['name'] as String,
email: json['email'] as String?,
age: json['age'] as int?,
);
// No structure validation!
// Null errors if keys missing!
}
โจ Pattern Matching (Dart 3+)
factory User.fromJson(Map<String, Object?> json) {
return switch (json) {
{
'id': final int id,
'name': final String name,
'email': final String? email,
'age': final int? age,
} => User(
id: id,
name: name,
email: email,
age: age,
),
_ => throw FormatException('Invalid: $json'),
};
}
๐งช Pattern Tester
Select a test case to see pattern matching in action!
Step 4
Handling Nested JSON
๐งฉ Nested Structure Explorer
Complex JSON:
๐ Generated Nested Models:
class User {
final int id;
final Profile profile;
final List<Post> posts;
// ...
}
class Profile {
final String name;
final Address address;
// ...
}
class Address {
final String street;
final String city;
final String country;
// ...
}
class Post {
final int id;
final String title;
// ...
}
Step 5
Serialization: Object โ JSON
๐ Round-Trip Tester
See JSON โ Object โ JSON in action:
1๏ธโฃ Original JSON
2๏ธโฃ Dart Object
// Parsed object representation
3๏ธโฃ Re-serialized JSON
// jsonEncode result
๐
Build: Wikipedia Data Models
๐ WikiSummary Model
๐ SearchResults Model
๐ท๏ธ TitlesSet Model
๐งช Test with Real Wikipedia JSON
๐ค Model Usage Preview
Test the Wikipedia JSON to see model parsing!
๐ฎ JSON Challenge: Data Model Master Quiz
๐ฆ Score: 0/6
โก Streak: 0
โฑ Time: 0s
๐ฌ Parsing: 0%