input.json
order.dart
class Order { final String orderId; final Customer customer; final double total; final DateTime createdAt; final List<Item> items; const Order({ required this.orderId, required this.customer, required this.total, required this.createdAt, required this.items, }); factory Order.fromJson(Map<String, dynamic> json) { return switch (json) { { "order_id": String orderIdValue, "customer": Map<String, dynamic> customerValue, "total": num totalValue, "created_at": String createdAtValue, "items": List<dynamic> itemsValue, } => Order( orderId: orderIdValue, customer: Customer.fromJson(customerValue), total: totalValue.toDouble(), createdAt: DateTime.parse(createdAtValue), items: itemsValue.map((e) => Item.fromJson(e as Map<String, dynamic>)).toList(), ), _ => throw const FormatException('Invalid Order JSON'), }; } Map<String, dynamic> toJson() => { "order_id": orderId, "customer": customer.toJson(), "total": total, "created_at": createdAt.toIso8601String(), "items": items.map((e) => e.toJson()).toList(), }; } class Customer { final String firstName; final String email; final bool isVerified; const Customer({ required this.firstName, required this.email, required this.isVerified, }); factory Customer.fromJson(Map<String, dynamic> json) { return switch (json) { { "first_name": String firstNameValue, "email": String emailValue, "is_verified": bool isVerifiedValue, } => Customer( firstName: firstNameValue, email: emailValue, isVerified: isVerifiedValue, ), _ => throw const FormatException('Invalid Customer JSON'), }; } Map<String, dynamic> toJson() => { "first_name": firstName, "email": email, "is_verified": isVerified, }; } class Item { final String sku; final int quantity; final double price; final String? note; const Item({ required this.sku, required this.quantity, required this.price, this.note, }); factory Item.fromJson(Map<String, dynamic> json) { return switch (json) { { "sku": String skuValue, "quantity": int quantityValue, "price": num priceValue, } => Item( sku: skuValue, quantity: quantityValue, price: priceValue.toDouble(), note: json["note"] == null ? null : json["note"] as String, ), _ => throw const FormatException('Invalid Item JSON'), }; } Map<String, dynamic> toJson() => { "sku": sku, "quantity": quantity, "price": price, "note": note, }; }
Convert JSON to Dart
Paste a JSON object into the editor to generate Dart models as you type. Copy the result into your project; the default output needs no package or build step.
Output behavior
The generated classes have final fields, const constructors, switch-based fromJson factories, and toJson methods. Array elements are merged before inference, so keys absent from some objects become nullable. Nested objects become named classes.
Before using it
Decode your API response with dart:convert, cast it to Map<String, dynamic>, and pass it to the root class’s fromJson factory. Set the root class name before copying if your project uses a different model name.