About this tool
Paste a JSON response and get Dart model classes for Flutter or Dart on the server. Classes are null-safe: required fields use required this.x, and values that were null or missing become nullable (String?). Keys like full_name become fullName.
Two styles
- fromJson / toJson writes the conversion code by hand: no packages and no build step. Nested objects call their own
fromJson, lists map each item, and numbers are read throughnumso1and1.0both work. - json_serializable adds
@JsonSerializable()and@JsonKey(name: …), and the code is generated bydart run build_runner build. Addjson_annotationto dependencies andjson_serializableandbuild_runnerto dev dependencies.
Using the classes
import 'dart:convert';
final root = Root.fromJson(jsonDecode(body) as Map<String, dynamic>);
final body2 = jsonEncode(root.toJson());
How types are inferred
- Every object becomes its own named type. Names come from the key (
address→Address), and items of an array get the singular (orders→Order). - For arrays of objects, all items are merged into one type: a field missing from some items becomes optional, and a field that is sometimes
nullbecomes nullable. - Integers and decimals in the same field merge to a decimal type. Fields with mixed types (say a number in one item and a string in another) fall back to the language’s “any” type.
- Identical nested shapes are reused instead of generating duplicates.
The code is generated from examples, so it can only be as accurate as the sample. Paste a response that includes optional fields and nulls (or several items) for the best result, then check the types against the API documentation. Generated in your browser: your JSON is not uploaded.
FAQ
How do I convert JSON to a Dart class?
Paste the JSON above. A class with a fromJson factory and a toJson method is generated for each object.
Should I use json_serializable?
For large models it saves maintenance: the code is regenerated when you change a class. For a few models, the hand-written style needs no build step.
Why is a number read as num?
JSON doesn’t distinguish 1 from 1.0, and Dart’s jsonDecode may return either int or double, so the code casts to num and converts.
Is the code null-safe?
Yes. It targets Dart 3 with sound null safety.