Module 2
Topic 3

JSON

Understanding JSON — the universal data format for APIs and how to work with it in Flutter.

Flutter Docs – JSON Serialization JSON.org
What Is JSON?

JSON (JavaScript Object Notation) is a lightweight data-interchange format that is easy for humans to read and write, and easy for machines to parse and generate. It's the most common format used by REST APIs to send and receive data.

🎯 Why JSON?

  • Universal – Used by almost every API and programming language
  • Human-readable – Easy to understand and debug
  • Lightweight – Minimal overhead compared to XML
  • Language-independent – Works with Dart, JavaScript, Python, etc.

JSON represents data as key-value pairs and ordered lists . Here's a simple example:

JSON Data Types

JSON supports a small set of data types. Understanding these is essential for parsing JSON in Flutter.

🔤 String

Text enclosed in double quotes

🔢 Number

Integer or floating-point values

✅ Boolean

True or false values

📦 Object

Key-value pairs enclosed in { }

📋 Array

Ordered list enclosed in [ ]

🚫 Null

Empty or missing value

Parsing JSON in Dart

Dart provides the dart:convert library with jsonDecode and jsonEncode functions for working with JSON.

📦 Important Types

  • jsonDecode() – Converts JSON string to Dart object
  • jsonEncode() – Converts Dart object to JSON string
  • Map<String, dynamic> – Type for JSON objects
  • List<dynamic> – Type for JSON arrays
  • dynamic – Values can be String, int, bool, null, or nested Map/List
Parsing Complex JSON

Most real-world APIs return complex JSON with nested objects and arrays. Here's how to parse them.

JSON to Dart Objects

For better type safety, you should convert JSON data into strongly-typed Dart classes . This makes your code more maintainable and less error-prone.

✅ Best Practice

Always define strongly-typed Dart classes with fromJson and toJson methods. This provides type safety, IDE autocomplete, and makes your code more maintainable.

Handling Lists of Objects

Most APIs return lists of objects . Here's how to parse a list of albums.

Complete Example: Albums App

Here's a complete Flutter app that fetches JSON data from an API, parses it into Dart objects, and displays it in a list.

Step-by-Step Explanation
1.
Define the Model – Create an Album class with typed properties and a fromJson factory constructor.
2.
Parse the JSON – Use jsonDecode() to convert the JSON string into a Dart object ( Map or List ).
3.
Convert to Typed Objects – Use the fromJson factory constructor to convert Map objects into typed Album objects.
4.
Handle Lists – Create a fromJsonList method to convert a list of JSON objects into a typed list.
5.
Display in UI – Use the typed list with FutureBuilder to display the data in a list view.
Common Mistakes
❌ Mistake 1: Forgetting to cast types

jsonDecode() returns dynamic . You must cast to Map<String, dynamic> or List<dynamic> before accessing values.

✅ Correct: Always cast after parsing

Use jsonDecode(response.body) as Map<String, dynamic> or jsonDecode(response.body) as List<dynamic> .

❌ Mistake 2: Not handling null values

JSON can contain null values. Accessing them without handling will crash your app.

✅ Correct: Use null-aware operators

Use json['field'] as String? or json['field'] ?? 'default' to handle null values safely.

❌ Mistake 3: Not checking status codes

Always check response.statusCode before parsing the JSON. A 404 or 500 error will contain error information, not the data you expect.

✅ Correct: Always check status codes

Use if (response.statusCode == 200) before parsing, and throw exceptions for other status codes.

🎯 Key Takeaway

JSON is the universal data format for APIs. In Flutter, use dart:convert to parse JSON with jsonDecode() and jsonEncode() . Always convert JSON to typed Dart classes for type safety and maintainability.