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 classes as you type. The default output has final fields, a const constructor, switch-based fromJson parsing, and toJson. It needs no package or build step.
Array elements are merged before inference, so a key missing from some objects becomes nullable. Nested objects become named classes. Use the settings to change the root class name, numeric casting, and DateTime detection before copying the Dart file.