JSON to Dart Class
Generate a Dart class from a JSON sample — typed fields, fromJson, and toJson.
or drop a file here
Dart class
class MyModel {
final int id;
final String name;
final String email;
final double score;
final bool active;
final List<String> tags;
final Address address;
const MyModel({
required this.id,
required this.name,
required this.email,
required this.score,
required this.active,
required this.tags,
required this.address,
});
factory MyModel.fromJson(Map<String, dynamic> json) => MyModel(
id: (json['id'] as num).toInt(),
name: json['name'] as String,
email: json['email'] as String,
score: (json['score'] as num).toDouble(),
active: json['active'] as bool,
tags: (json['tags'] as List).cast<String>(),
address: Address.fromJson(json['address'] as Map<String, dynamic>),
);
Map<String, dynamic> toJson() => {
'id': id,
'name': name,
'email': email,
'score': score,
'active': active,
'tags': tags,
'address': address.toJson(),
};
}
class Address {
final String street;
final String city;
const Address({
required this.street,
required this.city,
});
factory Address.fromJson(Map<String, dynamic> json) => Address(
street: json['street'] as String,
city: json['city'] as String,
);
Map<String, dynamic> toJson() => {
'street': street,
'city': city,
};
}How JSON to Dart class generation works
Each JSON field is mapped to a typed Dart property: strings → String, integers → int, decimals → double, booleans → bool, arrays → List<T>, nested objects → nested classes. The generator creates a constructor with named required parameters, a fromJson factory, and a toJson method.
For TypeScript types from JSON, see JSON to TypeScript. For Go structs, try JSON to Go Struct. For Rust, see JSON to Rust Struct.
Built and maintained by Meet Shah · Last updated
What this tool is used for
- Generating a model class from an API response for a Flutter app.
- Producing fromJson and toJson methods rather than writing them by hand.
- Getting a starting model for an endpoint with no schema.
- Creating classes for a fixture that match production shapes.
- Seeing how a payload's nesting maps onto nested Dart classes.
Frequently Asked Questions
- How do JSON types map to Dart?
- Strings to `String`, booleans to `bool`, whole numbers to `int`, fractional ones to `double`, arrays to `List<T>` and objects to a generated class. A `null` carries no type, so it becomes `dynamic`.
- Why does an int sometimes need to be double?
- Because Dart's `int` and `double` are distinct types with no implicit widening. A field that arrives as `0` in one response and `0.5` in another must be declared `double`, or the parse throws at runtime on the whole-number case.
- What generates fromJson and toJson?
- They are emitted as plain methods, so the class works with `dart:convert` and no build step. `json_serializable` generates equivalent code from annotations, which is worth adopting once the number of models grows.
- How is an array of objects handled?
- The elements are merged before the class is generated, so a key present on only some of them still becomes a field. Reading only the first element produces a model that drops data the rest of the array carries.
- Should the fields be nullable?
- Under sound null safety, any field an API can omit must be declared with `?` or the constructor will throw on a real response. JSON cannot tell you which those are — that decision comes from the API contract.
- Is a value class better than a plain class?
- For models compared or used as map keys, yes — Dart's default equality is by reference, so two identical parses are not equal. `freezed` or a manual `==` and `hashCode` fixes that; the generated class is the starting point.
Common errors and gotchas
- Accepting inferred nullability, which one sample cannot determine and which Dart's null safety enforces.
- Letting a whole number become int when another response returns a double.
- Assuming generated code handles a missing key, where a non-nullable field will throw on decode.
- Shipping generated class names taken from keys, which rarely read idiomatically.
- Overlooking that a JSON number above 2^53 has already lost precision before Dart sees it.
Related Converters tools
JSON to YAML
Convert JSON into clean, readable YAML instantly.
YAML to JSON
Convert YAML configuration into valid JSON.
JSON to XML
Convert JSON structures into nested XML markup.
HWB to HEX Converter
Convert an HWB color to HEX.
JSON to SQL
Turn a JSON array of objects into SQL INSERT statements.
CSV to XML
Convert CSV rows into structured XML records.
XML to CSV
Flatten repeated XML records into CSV rows.
INI to JSON
Parse INI config sections and keys into JSON.