Convert JSON to Dart class!

Input JSON to generate Dart class code with fromJson/toJson, including nested objects! For Flutter projects.
JSON
Result
 

Convert JSON to Dart class!

Turn JSON into Dart classes with a fromJson factory and a toJson method. The code is plain Dart with no code-generation packages, so there is no build_runner step.

It fits Flutter apps that pass raw Map<String, dynamic> responses from http or dio into widgets and want proper model classes instead, or small projects that need working serialization before adopting json_serializable.

How to use

  1. Paste the JSON to convert.
  2. Enter the top-level class name in Type name (for example Store).
  3. Paste the output into a file under lib/models/; no imports are needed.
  4. Create objects with Store.fromJson(jsonDecode(response.body) as Map<String, dynamic>) and send them with jsonEncode(store.toJson()).

Example

Input (JSON)
{
  "storeId": 77,
  "storeName": "Mapo Kimbap",
  "isOpen": true,
  "rating": 4.7,
  "menuItems": [
    { "name": "Tuna Kimbap", "price": 4500, "soldOut": false },
    { "name": "Ramyeon", "price": 5000, "soldOut": true, "spicy": 2 }
  ]
}
Output (Dart)
class Store {
  final int storeId;
  final String storeName;
  final bool isOpen;
  final double rating;
  final List<MenuItem> menuItems;

  Store({
    required this.storeId,
    required this.storeName,
    required this.isOpen,
    required this.rating,
    required this.menuItems,
  });

  factory Store.fromJson(Map<String, dynamic> json) => Store(
        storeId: json['storeId'] as int,
        storeName: json['storeName'] as String,
        isOpen: json['isOpen'] as bool,
        rating: (json['rating'] as num).toDouble(),
        menuItems: (json['menuItems'] as List<dynamic>).map((e) => MenuItem.fromJson(e as Map<String, dynamic>)).toList(),
      );

  Map<String, dynamic> toJson() => {
        'storeId': storeId,
        'storeName': storeName,
        'isOpen': isOpen,
        'rating': rating,
        'menuItems': menuItems.map((e) => e.toJson()).toList(),
      };
}

class MenuItem {
  final String name;
  final int price;
  final bool soldOut;
  final int? spicy;

  MenuItem({
    required this.name,
    required this.price,
    required this.soldOut,
    this.spicy,
  });

  factory MenuItem.fromJson(Map<String, dynamic> json) => MenuItem(
        name: json['name'] as String,
        price: json['price'] as int,
        soldOut: json['soldOut'] as bool,
        spicy: json['spicy'] as int?,
      );

  Map<String, dynamic> toJson() => {
        'name': name,
        'price': price,
        'soldOut': soldOut,
        'spicy': spicy,
      };
}

Elements of the menuItems array become a MenuItem class, converted inside fromJson with .map(...).toList(). rating is read as (json['rating'] as num).toDouble() so an integer value is still safe, and spicy, present only on some menu items, is int? without required in the constructor.

Supported features

  • All fields are final; constructors use named parameters ({required this.x}).
  • Hand-written factory X.fromJson(Map<String, dynamic> json) and Map<String, dynamic> toJson() with no package dependencies.
  • Nested objects and object arrays call the child class's fromJson / toJson.
  • double fields are read via (as num).toDouble(), so a JSON integer like 4 doesn't throw a cast error.
  • Fields missing from some items or containing null become nullable (int?) and pass null through in fromJson.
  • Mixed-type values are dynamic; Dart reserved words such as class or is get a trailing underscore.
  • A top-level array produces typedef Root = List<RootItem>;.

Use cases

API models for Flutter screens

Carrying a Map through FutureBuilder or Riverpod/Bloc state hides key typos until runtime. With generated classes you get completion and type checks on store.menuItems.first.price.

Local caching

Store jsonEncode(store.toJson()) in shared_preferences or a file and restore it with fromJson at startup for a cache in a few lines.

A draft before json_serializable

Confirm the field list and types first; as the project grows, add @JsonSerializable() to the same fields and move to code generation.

Notes and limitations

  • Types are inferred with the browser's JSON parser, which reads 5000.0 as the integer 5000. Dart's jsonDecode keeps 5000.0 as a double, so as int throws. Change price or coordinate fields that may carry a decimal point to double.
  • toJson writes nullable fields even when they are null ('spicy': null). If your server rejects null keys, leave them out conditionally.
  • Date strings stay String; convert them yourself with DateTime.parse(json['createdAt'] as String).
  • No copyWith, ==/hashCode or toString is generated. Consider freezed or equatable if you need them.
  • dynamic fields hold values unchecked; replace them once you know the real type.

FAQ

Can it generate json_serializable or freezed code?

The output is hand-written-style fromJson/toJson without packages. Take the field declarations, add @JsonSerializable() and part 'store.g.dart';, and run dart run build_runner build to switch to code generation.

I get type 'int' is not a subtype of type 'double'.

That happens when code casts with as double and the JSON value is an integer. Generated double fields use (as num).toDouble() to avoid it; apply the same pattern to any field you edited by hand.

How do I decode a list response ([ {...}, {...} ])?

A top-level array yields a typedef and an element class. Call fromJson per element: (jsonDecode(body) as List<dynamic>).map((e) => RootItem.fromJson(e as Map<String, dynamic>)).toList().

Does it work in old projects without null safety?

The output uses sound null safety syntax (int?, required) from Dart 2.12 onward. Pre-null-safety projects need the ? and required removed to compile.