JSON to Dart class (Flutter)
The tool writes a class per object with final fields, a named-parameter constructor and methods to convert from and to JSON.
Loading the tool…
Processing and privacy: The result is calculated inside your browser and your data is not sent to any server.
How to use it
- Enter json input and root type name.
- The result appears straight away and updates as you edit.
- Copy the result, or copy a link that keeps the same values to share it.
How it is calculated
int, double, String, bool, List<T> and dynamic, with a class per object. null or a missing key makes the type nullable, such as int?. Numbers are read through num and then toInt or toDouble, because JSON may send 10 instead of 10.0, and nested objects use their own fromJson.
Example
Inputs: Root type name = Root. Result: Generated code: class Root { final int id; final String name; final String email; final bool isActive; final double balance; final List<String> tags; final Address address; final List<Order> orders; Root({ required this.id, required this.name, required this.email, required this.isActive, required this.balance, required this.tags, required this.address, required this.orders, }); factory Root.fromJson(Map<String, dynamic> json) => Root( id: (json['id'] as num).toInt(), name: json['name'] as String, email: json['email'] as String, isActive: json['is_active'] as bool, balance: (json['balance'] as num).toDouble(), tags: (json['tags'] as List<dynamic>).map((e) => e as String).toList(), address: Address.fromJson(json['address'] as Map<String, dynamic>), orders: (json['orders'] as List<dynamic>).map((e) => Order.fromJson(e as Map<String, dynamic>)).toList(), ); Map<String, dynamic> toJson() => { 'id': id, 'name': name, 'email': email, 'is_active': isActive, 'balance': balance, 'tags': tags, 'address': address.toJson(), 'orders': orders.map((e) => e.toJson()).toList(), }; } class Address { final String city; final String postalCode; Address({ required this.city, required this.postalCode, }); factory Address.fromJson(Map<String, dynamic> json) => Address( city: json['city'] as String, postalCode: json['postal_code'] as String, ); Map<String, dynamic> toJson() => { 'city': city, 'postal_code': postalCode, }; } class Order { final String orderId; final double total; final String? coupon; Order({ required this.orderId, required this.total, this.coupon, }); factory Order.fromJson(Map<String, dynamic> json) => Order( orderId: json['order_id'] as String, total: (json['total'] as num).toDouble(), coupon: json['coupon'] as String?, ); Map<String, dynamic> toJson() => { 'order_id': orderId, 'total': total, 'coupon': coupon, }; }; Types generated: 3; Fields: 13.
{
"id": 1024,
"name": "Sara Ahmed",
"email": "sara@example.com",
"is_active": true,
"balance": 1520.75,
"tags": ["vip", "riyadh"],
"address": { "city": "Riyadh", "postal_code": "12211" },
"orders": [
{ "order_id": "A-1", "total": 99.5, "coupon": "EID10" },
{ "order_id": "A-2", "total": 120, "coupon": null }
]
}class Root {
final int id;
final String name;
final String email;
final bool isActive;
final double balance;
final List<String> tags;
final Address address;
final List<Order> orders;
Root({
required this.id,
required this.name,
required this.email,
required this.isActive,
required this.balance,
required this.tags,
required this.address,
required this.orders,
});
factory Root.fromJson(Map<String, dynamic> json) => Root(
id: (json['id'] as num).toInt(),
name: json['name'] as String,
email: json['email'] as String,
isActive: json['is_active'] as bool,
balance: (json['balance'] as num).toDouble(),
tags: (json['tags'] as List<dynamic>).map((e) => e as String).toList(),
address: Address.fromJson(json['address'] as Map<String, dynamic>),
orders: (json['orders'] as List<dynamic>).map((e) => Order.fromJson(e as Map<String, dynamic>)).toList(),
);
Map<String, dynamic> toJson() => {
'id': id,
'name': name,
'email': email,
'is_active': isActive,
'balance': balance,
'tags': tags,
'address': address.toJson(),
'orders': orders.map((e) => e.toJson()).toList(),
};
}
class Address {
final String city;
final String postalCode;
Address({
required this.city,
required this.postalCode,
});
factory Address.fromJson(Map<String, dynamic> json) => Address(
city: json['city'] as String,
postalCode: json['postal_code'] as String,
);
Map<String, dynamic> toJson() => {
'city': city,
'postal_code': postalCode,
};
}
class Order {
final String orderId;
final double total;
final String? coupon;
Order({
required this.orderId,
required this.total,
this.coupon,
});
factory Order.fromJson(Map<String, dynamic> json) => Order(
orderId: json['order_id'] as String,
total: (json['total'] as num).toDouble(),
coupon: json['coupon'] as String?,
);
Map<String, dynamic> toJson() => {
'order_id': orderId,
'total': total,
'coupon': coupon,
};
}Limits and caveats
- It does not use json_serializable or freezed, so the code stands alone without build_runner.
- A key that is not a valid name becomes camelCase, and the original key stays inside fromJson and toJson.
Questions about this tool
Why are numbers read through num?
jsonDecode returns 10 as int and 10.5 as double, so reading through num avoids a cast error when the shape varies.
How do I use it with an API response?
Call Root.fromJson(jsonDecode(response.body)) to read, and jsonEncode(root.toJson()) to send.
Is my JSON sent to a server?
No. The conversion runs in your browser and the JSON never leaves your device.
