Dart classes from a JSON sample
Paste a JSON document and get Dart model classes for it. The default output needs no build step: each class carries its own fromJson factory and toJson method that read and write a Map<String, dynamic>. For projects already on build_runner, two checkboxes switch to json_serializable or freezed annotations instead.
Example
class Order {
Order({
required this.sku,
required this.qty,
this.gift,
});
final String sku;
final int qty;
final bool? gift;
factory Order.fromJson(Map<String, dynamic> json) => Order(
sku: json["sku"],
qty: json["qty"],
gift: json["gift"],
);
Map<String, dynamic> toJson() => {
"sku": sku,
"qty": qty,
"gift": gift,
};
}
gift is bool? and not required because only some orders in the sample carry it.
Code generation paths
json_serializable annotations adds @JsonSerializable() to each class and rewrites fromJson to call the generated _$OrderFromJson. Set part file so the part 'models.g.dart'; line matches your file name. freezed writes the class as an @freezed abstract class with a const factory constructor; it pairs with json_serializable annotations for JSON support. copyWith() adds a copyWith method to the hand-written path; freezed generates its own.
Limits
Dart has no union type, so a field that mixes types in the sample becomes dynamic. Repeated string values become an enum with a value map when enum inference is on. Dates become DateTime with DateTime.parse in fromJson when date inference is on.