JSON to Dart Class
Generate Dart classes with fromJson/toJson for Flutter and server-side Dart. No packages required.
fromJson / toJson methods. Paste output directly into your .dart file.What is Dart JSON Serialization?
Dart does not have built-in JSON serialization like some languages. Instead, you write fromJson factory constructors and toJson methods in your model classes to convert between Map<String, dynamic> (what jsonDecode() returns) and your typed Dart objects.
In Flutter apps, this pattern is used to parse HTTP responses from REST APIs, decode data from shared preferences or local storage, and deserialize notifications or WebSocket messages. Writing these classes by hand for every API response is repetitive — this tool generates the complete class hierarchy from your JSON instantly, with no code generation step required.
JSON to Dart Class Example
Input JSON:
{
"id": 1,
"name": "Alice",
"email": "alice@example.com",
"address": { "city": "Berlin", "zip": "10115" },
"tags": ["flutter", "dart"]
}Generated Dart class:
class Address {
final String? city;
final String? zip;
Address({
this.city,
this.zip,
});
factory Address.fromJson(Map<String, dynamic> json) => Address(
city: json['city'] as String?,
zip: json['zip'] as String?,
);
Map<String, dynamic> toJson() => {
'city': city,
'zip': zip,
};
}
class Root {
final int? id;
final String? name;
final String? email;
final Address? address;
final List<dynamic>? tags;
Root({
this.id,
this.name,
this.email,
this.address,
this.tags,
});
factory Root.fromJson(Map<String, dynamic> json) => Root(
id: json['id'] as int?,
name: json['name'] as String?,
email: json['email'] as String?,
address: json['address'] != null
? Address.fromJson(json['address'] as Map<String, dynamic>)
: null,
tags: json['tags'],
);
Map<String, dynamic> toJson() => {
'id': id,
'name': name,
'email': email,
'address': address?.toJson(),
'tags': tags,
};
}Use with Dio (Flutter)
final dio = Dio();
final response = await dio.get('https://api.example.com/user/1');
final user = Root.fromJson(response.data as Map<String, dynamic>);
print(user.name);Type Mapping
| JSON Type | Dart Type | Notes |
|---|---|---|
| string | String? | Nullable for safety |
| integer | int? | Dart int (platform native) |
| float | double? | Dart double |
| boolean | bool? | Nullable |
| null | dynamic | Most permissive type |
| array | List<T>? | Nullable list |
| object | class | New named Dart class |