JSON to Dart Class Generator
Last updated:
Jsonic's JSON to Dart Class generator converts a JSON object into Dart class definitions. Each class is generated with final fields, a named constructor with required parameters, a factory fromJson constructor that reads from a Map<String, dynamic>, and a toJson method that returns a Map<String, dynamic>. JSON strings map to String, integers to int, decimals to double, booleans to bool, and null values to the nullable equivalent (e.g. String?). JSON arrays become List<T> with an explicit type cast. Nested JSON objects generate separate named classes declared before the parent class. Snake_case JSON keys are converted to camelCase Dart field names, while the original key is preserved in fromJson and toJson for correct serialization. All processing runs in your browser with no data upload.
How to convert JSON to a Dart class
- Paste your JSON object into the left panel, or click Example to load a sample.
- Optionally rename the root class (default: Root).
- Click Generate.
- Copy the generated Dart code.
- Add the class file to your Flutter or Dart project and use fromJson to deserialize API responses.
FAQ
Does this work with Flutter?
Yes. The generated code uses only standard Dart classes with no external dependencies. Each class includes a factory fromJson(Map<String, dynamic> json) constructor and a toJson() method, which are the standard pattern for handling JSON in Flutter apps that use the http package. Call fromJson with the decoded response body from jsonDecode() to get a typed model object.
How are JSON types mapped to Dart types?
JSON string → String, JSON integer → int, JSON float → double, JSON boolean → bool, JSON null → nullable type (e.g. String?), JSON array → List<T> where T is inferred from the array elements, JSON object → a named Dart class. The nullable suffix (?) is applied whenever a field is observed as null or absent in the JSON.
How is snake_case converted to camelCase?
Snake_case JSON keys such as user_id are converted to camelCase Dart field names such as userId, following Dart and Flutter naming conventions. The original JSON key (user_id) is preserved in the fromJson and toJson methods so serialization remains correct. Hyphenated and space-separated keys are also converted to camelCase.
How do I use json_serializable instead?
This tool generates a manual implementation without any external packages. If you prefer code generation via json_serializable, add the @JsonSerializable() annotation to the class and a part directive, then run dart run build_runner build. The field structure generated here is compatible — you can add the annotation and remove the manual fromJson and toJson bodies to migrate.
How are arrays handled?
JSON arrays become List<T> fields where T is inferred from the element types in the array. The fromJson method uses List<T>.from() with an explicit cast to ensure type safety at runtime. Arrays of objects produce a map call that runs fromJson on each element. If the array is empty or contains mixed types, List<dynamic> is used as the element type.
What about the json_annotation package?
The generated code does not require json_annotation or any other package — it uses only Dart core types. The fromJson and toJson implementations are written manually using standard Map operations. This makes the output dependency-free and suitable for any Dart or Flutter project without additional build tooling.