๐Ÿ“ฆ Lesson 9: JSON & Data Models

โฑ 35-45 min ๐Ÿ“Š Intermediate ๐Ÿ”ฌ Data Layer ๐Ÿ”ฅ Essential 2026 ๐Ÿ”— Official Source
๐Ÿ“ฆ

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

๐Ÿท๏ธ 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%

Loading question...