"category": "code-generator",
JSON to Dart Class Generator
"tldr": Paste JSON and get Dart classes with typed fields and fromJson factory constructors - ready for Flutter apps.
Paste a JSON payload and get Dart model classes with final typed fields, const constructors, and fromJson factory constructors that handle nested objects and typed lists - the boilerplate every Flutter project needs for its API layer.
The generated fromJson casts explicitly (as String, as int, (json[...] as num).toDouble()), which surfaces type mismatches as clear errors at the parse boundary instead of mysterious failures deeper in the widget tree.
How to convert JSON to Dart classes
- 1Paste a representative JSON payload into the input panel.
- 2Each nested object becomes its own class with a fromJson factory.
- 3Doubles are decoded via (x as num).toDouble() - JSON integers like 59 decode safely into double fields.
- 4Copy into your Flutter project and call Root.fromJson(jsonDecode(body)).
Convert JSON to Dart class in code
import 'dart:convert';
import 'package:http/http.dart' as http;
final res = await http.get(Uri.parse(url));
final order = Root.fromJson(jsonDecode(res.body) as Map<String, dynamic>);
print(order.customer.name);final list = (jsonDecode(body) as List<dynamic>)
.map((e) => Root.fromJson(e as Map<String, dynamic>))
.toList();Frequently asked questions
Why the (as num).toDouble() dance for doubles?
JSON has one number type but Dart distinguishes int and double, and jsonDecode produces int for whole numbers - so a price of 60 arrives as int and a direct 'as double' cast throws. Casting through num handles both cases safely; it is the standard Flutter idiom.
Should I use json_serializable or freezed instead?
For large apps, yes - json_serializable generates this code from annotations at build time, and freezed adds immutability and unions. The classes generated here are perfect for small projects, quick prototypes, or as the starting shape you annotate for those packages.
How do I handle nullable fields?
Make the type nullable and drop the cast strictness: final String? notes; with notes: json['notes'] as String?. With Dart null safety, the compiler then forces every consumer to handle the null case.
Where is toJson?
The generator focuses on the consuming direction (fromJson), which is 90% of app code. Adding toJson is mechanical: return a map of field names to values, calling .toJson() on nested models and .map((e) => e.toJson()).toList() on lists.
Last updated:
You might also need
Free and in-browser, like everything on JSON Console. Browse all tools →