Tool Kumar tool
JSON to Dart
Generate Dart models for a Flutter API response. The output has final fields, const constructors, a fromJson factory and a toJson method for each nested class.
How to use this tool
- Paste a representative response, including records whose fields differ.
- Review the generated output and change the root name if needed.
- Copy or download the code, then check the types against your API contract.
Worked example
This output comes from the built-in converter. The two item prices infer a floating-point field; the null and string notes infer a nullable field.
Input
{
"user-id": 42,
"profile": {
"city": "London",
"created_at": "2026-10-14T09:00:00Z"
},
"items": [
{
"price": 12,
"note": null
},
{
"price": 12.5,
"note": "gift"
}
]
}Output
class Root {
final int userId;
final Profile profile;
final List<Item> items;
const Root({
required this.userId,
required this.profile,
required this.items,
});
factory Root.fromJson(Map<String, dynamic> json) => Root(
userId: json['user-id'] as int,
profile: Profile.fromJson(json['profile'] as Map<String, dynamic>),
items: (json['items'] as List<dynamic>).map((e) => Item.fromJson(e as Map<String, dynamic>)).toList(),
);
Map<String, dynamic> toJson() => {
'user-id': userId,
'profile': profile.toJson(),
'items': items.map((e) => e.toJson()).toList(),
};
}
class Profile {
final String city;
final String createdAt;
const Profile({
required this.city,
required this.createdAt,
});
factory Profile.fromJson(Map<String, dynamic> json) => Profile(
city: json['city'] as String,
createdAt: json['created_at'] as String,
);
Map<String, dynamic> toJson() => {
'city': city,
'created_at': createdAt,
};
}
class Item {
final double price;
final String? note;
const Item({
required this.price,
this.note,
});
factory Item.fromJson(Map<String, dynamic> json) => Item(
price: (json['price'] as num).toDouble(),
note: json['note'] as String?,
);
Map<String, dynamic> toJson() => {
'price': price,
'note': note,
};
}
Use the model in Flutter
Import dart:convert and paste the generated classes into a model file. Pass the decoded response map to Root.fromJson. jsonEncode calls the generated toJson method.
If your project already uses json_serializable, adapt the fields to that workflow rather than maintaining two sets of generated JSON methods.
import 'dart:convert';
final model = Root.fromJson(jsonDecode(responseBody) as Map<String, dynamic>);
final text = jsonEncode(model.toJson());Options and edge cases
- JSON keys such as user-id map to camelCase fields. The generated JSON methods keep the original keys.
- Integer and fractional prices merge into double. The decoder casts through num and calls toDouble, so both sample values work.
- Null plus a string produces String?. A field seen only as null stays dynamic because its intended type is unknown.
- Dates stay String. Review DateTime parsing yourself. Dart compiled for the web cannot represent every large integer exactly.
Private by default
Your document stays in this browser. Tool Kumar parses, converts and creates downloads on your device, with no account or upload step.
JSON to Dart questions
- Does this generate json_serializable annotations?
- No. It writes the fromJson and toJson methods directly. There are no @JsonSerializable annotations, part files or build_runner step.
- How does it handle null safety?
- Nulls and fields missing in some records make inferred fields nullable. Non-null fields use required constructor arguments. Null-only fields use dynamic.
- Can it convert arrays of nested objects?
- Yes. The sample produces List<Item>, maps each input element through Item.fromJson and calls toJson on each item when encoding.
- Do I need a Flutter package?
- The generated models use Dart core types. Decode response text with dart:convert, then call Root.fromJson.
Related tools
JSON to Swift models, JSON to Kotlin data classes, JSON Schema generator.