Developer tool
JSON to Dart Model Class Generator
Free, no sign-up. Runs in your browser; nothing is uploaded.
Options
JSON
Dart
The result appears here.
How to use the json to dart model class generator
From JSON to Flutter models
- Paste a JSON response into the input box. Use Sample if you just want to see the output.
- Enter a root class name. Nested objects become their own classes, named from their keys.
- Switch toJson off if you only read data and never send it back.
- Copy the Dart code into a file in your
modelsfolder.
What you get
Each class has final fields, a constructor with required named parameters, a factory fromJson(Map<String, dynamic> json) and, optionally, a toJson() method. Numbers are typed as int or double depending on the sample, and a double is read through (value as num).toDouble() so a JSON 1 does not crash a field that is usually 1.5.
Lists of objects are mapped with .map((e) => Item.fromJson(e as Map<String, dynamic>)).toList(). Lists of strings, numbers and booleans keep their element type.
Things to check
- Null in the sample produces a
dynamicfield, because the real type is unknown. Replace it once you know the type. - Mixed types in one key, such as a string in one record and a number in another, also fall back to
dynamic. - Reserved words like
classordefaultas keys get aValuesuffix, for exampleclassValue. - Dates arrive as strings. Convert them with
DateTime.parsein your own code if you need aDateTime. - If you add
json_serializablelater, keep the class names and replace the hand-written methods with annotations.
Good habits
Feed the tool a response that contains every optional field, run your app against real data, and watch for cast errors in the console. A wrong type in fromJson fails loudly at runtime, which is how you find fields the sample did not cover.
Frequently asked questions
Does it use json_serializable or freezed?
No. It writes plain Dart classes with a fromJson factory and a toJson method that you can paste straight into a project, with no code generation step and no extra packages.
How are snake_case keys handled?
Keys are converted to camelCase field names, such as user_id to userId. The original key is kept inside fromJson and toJson, so the JSON still maps correctly.
When is a field nullable?
A field is nullable when it holds null in the sample, or when it is missing from some objects in an array. Fields present with a real value in every object are marked required.
What if my JSON root is a list?
If the root is a list of objects, the tool builds the class for the item and tells you to map each entry with fromJson. A list of plain values cannot become a class.
Is my data uploaded?
No. The conversion happens locally in your browser tab.