Paste a JSON object or API response and get null-safe Dart model classes for Flutter: final fields, a named constructor, fromJson and toJson, copyWith and a class for every nested object. Choose plain Dart or json_serializable annotations. Everything runs in your browser.
Last updated:
Your JSON is parsed locally in this tab and never uploaded. Tip: Ctrl + Enter generates.
Advertisement
Advertisement
How to use the JSON to Dart Class
Paste your JSON into the left box or open a .json file. A sample is loaded so you can see the output right away.
Name the root class, for example User or ProductResponse.
Choose the output style: plain Dart with hand-written fromJson/toJson, or json_serializable annotations for build_runner.
Set the options: which fields are nullable, field naming, final fields, const constructor, copyWith, toString and DateTime parsing.
Copy or download the .dart file into your Flutter project and call User.fromJson(jsonDecode(body)).
Example: JSON to a Dart model class
Given this JSON with snake_case keys, a date, a nested object and a list:
The part 'root.g.dart'; line must match the file name of your Dart file.
Classes that contain other model classes get @JsonSerializable(explicitToJson: true), so nested objects are converted with their own toJson() instead of being left as instances.
Fields whose Dart name differs from the JSON key get @JsonKey(name: 'json_key').
Frequently asked questions
How do I use the generated class with an HTTP response?
Import dart:convert, decode the body and pass the map to the factory: final user = User.fromJson(jsonDecode(response.body) as Map<String, dynamic>). To send data back, call jsonEncode(user.toJson()). If the JSON is a top-level array, map each item with fromJson as shown in the generated comment.
Which fields become nullable?
By default a field gets a nullable type (for example String?) when its value is null somewhere in the sample or when the key is missing from some items of an array. All other fields are non-nullable and marked required in the constructor. You can also make every field nullable, which is safer for APIs that may omit keys.
Why are doubles parsed with (value as num).toDouble()?
jsonDecode returns an int for a number written without a decimal point, so a price of 3 arrives as int even if other items have 3.5. Casting with as double would throw, so the generator casts to num and converts with toDouble(), which accepts both.
Plain Dart or json_serializable: which should I pick?
Plain Dart needs no packages or code generation and is easy to read and debug. json_serializable is popular in larger apps: it writes the conversion code for you in a .g.dart file. With that style, add json_annotation to dependencies and json_serializable plus build_runner to dev_dependencies, then run dart run build_runner build.
How are JSON keys converted to Dart field names?
Keys are converted to lowerCamelCase as recommended by Effective Dart, so first_name becomes firstName. The original key is still used inside fromJson and toJson (or in @JsonKey(name: ...)), so the JSON is read and written unchanged. Reserved words such as class or default get a trailing underscore.
What happens with null-only values and mixed types?
If a value is always null, or mixes types such as string and number, the field is typed dynamic because Dart cannot know a more precise type from the sample. Empty objects become Map<String, dynamic>. Replace dynamic with the real type once you know it.
Is my JSON sent to a server?
No. The JSON is parsed and the Dart code is generated by JavaScript running in this page, so nothing is uploaded.
Are all the tools on FreeToolCabin tested for quality and accuracy?
Yes! Every tool on our site undergoes automated Q/A testing in real browser environments to ensure high precision, correct output, and zero errors. If you happen to spot a bug or have a suggestion, please contact us at [email protected] and our team will review it right away.