Toolvito

Works on this device

Dart JSON Model Generator

Paste a JSON sample, such as the reply of an API. You get a Dart class for each object, with typed fields, nullable types where the sample has null or lacks a key, nested classes and lists, a fromJson factory and a toJson method. The code needs no package. It is only written as text here; nothing is run or sent.

Enter your values

Your values

A JSON object, or a list of objects. Several objects in a list are joined, so keys that only some have become nullable.

In PascalCase, such as User or OrderResponse.

Your values are worked out in this browser. They are not sent to a server.

Your result will show here

Fill in the values, then choose Generate.

How to use the Dart model generator

  1. Paste a JSON object, or a list of objects, or open a .json file.
  2. Type the name of the main class, such as User.
  3. Choose whether only the fields that need it, or every field, are nullable, and whether the fields are final.
  4. Copy the code or download it as a .dart file. Read JSON with jsonDecode from dart:convert and pass the map to User.fromJson.

How the types are chosen

  • A string is String. A number written without a decimal point or an exponent is int; any other number is double. true and false are bool.
  • An object is a class of its own, named from its key in PascalCase (address becomes Address). An array is List<T>, with T from all its items. The items of a list of objects get one class, named from the key in the singular (orders becomes Order, categories becomes Category).
  • When a place has both int and double (25 and 12.75), it is double. A value and null give the nullable type (String?). Other mixes, and a key that is only null, give dynamic.
  • Objects in a list are joined: a key that only some of them have becomes nullable.
  • Field names are camelCase (user_name becomes userName). The JSON key is kept in fromJson and toJson, so the names can differ. A Dart word such as class gets Value added (classValue).

Worked example

In the example, total is 25 in one order and 12.75 in the other, so it is double, read with (json['total'] as num).toDouble(). coupon is only in the second order, so it is String?. nickname is only null, so it is dynamic. orders becomes List<Order>, read with .map((e) => Order.fromJson(e as Map<String, dynamic>)).toList(). The tool writes 4 classes (User, Address, Order, Item) with 18 fields.

Limits you should know

  • The types come only from the sample. A field that is a whole number in the sample but a decimal in other replies, or null only in other replies, needs a change by hand. Paste several objects in a list to give a better sample.
  • Dates stay String: JSON has no date type. Convert them with DateTime.parse where you need it.
  • Very large whole numbers (beyond 64 bits) become double, as jsonDecode reads them.
  • The code is written for Dart with null safety (Dart 2.12 and later). The tool does not run or compile it.
  • For classes in TypeScript, Kotlin, Java, Swift, Go, C# or Rust, use the Data Model Code Generator.

Related tools