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
Check these values
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.
Your result
How it was worked out
How to use the Dart model generator
- Paste a JSON object, or a list of objects, or open a .json file.
- Type the name of the main class, such as User.
- Choose whether only the fields that need it, or every field, are nullable, and whether the fields are final.
- Copy the code or download it as a .dart file. Read JSON with
jsonDecodefromdart:convertand pass the map toUser.fromJson.
How the types are chosen
- A string is
String. A number written without a decimal point or an exponent isint; any other number isdouble.trueandfalsearebool. - An object is a class of its own, named from its key in PascalCase (
addressbecomesAddress). An array isList<T>, with T from all its items. The items of a list of objects get one class, named from the key in the singular (ordersbecomesOrder,categoriesbecomesCategory). - When a place has both
intanddouble(25 and 12.75), it isdouble. A value andnullgive the nullable type (String?). Other mixes, and a key that is onlynull, givedynamic. - Objects in a list are joined: a key that only some of them have becomes nullable.
- Field names are camelCase (
user_namebecomesuserName). The JSON key is kept infromJsonandtoJson, so the names can differ. A Dart word such asclassgetsValueadded (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 withDateTime.parsewhere you need it. - Very large whole numbers (beyond 64 bits) become
double, asjsonDecodereads 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.