FormatterHQ

JSON to Dart Model Generator

Generate null-safe Dart model classes for Flutter from JSON: final fields, required named parameters, nested classes and fromJson / toJson methods.

Input JSON
Output DART
Ready.

How to use the JSON to Dart

  1. Paste your JSON sample (an object, or an array of objects) into the Input editor, or use Sample, Upload file or Load URL, or press Sample to try one.
  2. Set the Top-level class name, for example Task; it is converted to PascalCase. Nested types are named after their property keys.
  3. Leave Types only (no helpers) off to get fromJson, toJson and top-level parse functions; tick it for plain classes.
  4. Press Generate (it also re-runs automatically as you edit), then use Copy or Download.

JSON to Dart: null-safe models for Flutter

Dart's jsonDecode returns Map<String, dynamic>, which gives no type safety and no autocompletion. Flutter apps therefore wrap API responses in model classes with a fromJson factory. This JSON to Dart generator writes those classes from a sample: final fields, a constructor with named parameters marked required, nullable types where values can be missing, and conversion code for nested objects, lists and dates.

Example

{
  "id": 42,
  "title": "Write docs",
  "done": false,
  "priority": 2.5,
  "tags": ["docs"],
  "owner": { "name": "Ada", "email": null },
  "due": "2026-10-01T09:00:00Z",
  "subtasks": [
    { "title": "Outline", "estimate": 2 },
    { "title": "Draft" }
  ]
}
Task taskFromJson(String str) => Task.fromJson(json.decode(str));
String taskToJson(Task data) => json.encode(data.toJson());

class Task {
    final int id;
    final String title;
    // ...
    final DateTime due;
    final List<Subtask> subtasks;

    Task({ required this.id, required this.title, /* ... */ });

    factory Task.fromJson(Map<String, dynamic> json) => Task(
        id: json["id"],
        title: json["title"],
        priority: json["priority"]?.toDouble(),
        due: DateTime.parse(json["due"]),
        subtasks: List<Subtask>.from(json["subtasks"].map((x) => Subtask.fromJson(x))),
        // ...
    );
}

class Subtask {
    final String title;
    final int? estimate;
    Subtask({ required this.title, this.estimate });
}

priority uses ?.toDouble() because JSON numbers like 2 decode as int in Dart even when the field is a double; this conversion avoids a runtime type error. estimate is int? and not required. Dates are parsed with DateTime.parse and written back with toIso8601String().

Using the model in Flutter

final response = await http.get(Uri.parse(url));
final task = taskFromJson(response.body);
Text(task.title);

For a list endpoint, paste an array: the class describes one element and taskFromJson returns a List<Task>.

Handwritten style vs. code generation packages

The output is plain Dart with no dependencies, which is ideal for small apps and for learning how JSON mapping works. Larger Flutter projects often use json_serializable or freezed, which generate the same fromJson/toJson code at build time from annotations. The field list generated here is a quick start for those too: copy the fields, add @JsonSerializable() and run build_runner.

Immutable updates with copyWith

The generated fields are final, which suits Flutter's rebuild model, but you often need a changed copy, for example when a user toggles done. Add a copyWith method by hand:

Task copyWith({bool? done}) => Task(
    id: id, title: title, done: done ?? this.done, priority: priority,
    tags: tags, owner: owner, due: due, subtasks: subtasks,
);

Packages like freezed generate this automatically if your models grow. Keep the parameter list in sync when you add fields, since the compiler will not remind you about a forgotten property in a hand-written copyWith.

Type mapping

JSONDart
integer / decimalint / double
boolean / stringbool / String
ISO dateDateTime
arrayList<T>
optionalT?, not required
null onlydynamic

Common pitfalls

  • "type 'int' is not a subtype of type 'double'" – a field that is sometimes whole and sometimes decimal. Keep the toDouble() call or include both kinds of value in your sample.
  • Null errors at runtime – a field that was always present in the sample but is missing in real responses. Make it nullable and drop required.
  • Naming: keys like user_name become userName fields while fromJson keeps reading json["user_name"].

Format the result with dart format. For XML APIs, use XML to Dart. Check JSON with the JSON Validator or explore it in the JSON Viewer. Generation runs in your browser.

Frequently asked questions

Is the generated Dart code null-safe?
Yes. Required fields are non-nullable and marked required in the constructor; fields missing from some samples are nullable (T?) and optional.
Do I need json_serializable to use the output?
No. The generated fromJson and toJson methods are plain Dart with no dependencies beyond dart:convert.
Why is there a toDouble() call on number fields?
JSON numbers without a decimal point decode as int in Dart. Calling toDouble() prevents a type error when a double field receives a whole number.
How are nested lists converted?
Lists of objects are mapped element by element with the nested class’s fromJson, producing a typed List.

Last updated September 26, 2026

Related tools

XML to Dart Generate Dart classes with fromJson and toJson from a XML sample instantly, with nested types detected automatically. JSON to Elm Generate Elm type aliases with decoders from a JSON sample instantly, with nested types detected automatically. JSON to Flow Generate Flow Flow type definitions from a JSON sample instantly, with nested types detected automatically. JSON to Go Generate Go structs with json tags from a JSON sample instantly, with nested types detected automatically. JSON to Haskell Generate Haskell records with Aeson instances from a JSON sample instantly, with nested types detected automatically. JSON to Java Generate Java POJO classes with getters and setters from a JSON sample instantly, with nested types detected automatically. JSON to JavaScript PropTypes Generate JavaScript PropTypes React PropTypes definitions from a JSON sample instantly, with nested types detected automatically. JSON to JSON Schema Generate JSON Schema a JSON Schema document from a JSON sample instantly, with nested types detected automatically. JSON to Kotlin Generate Kotlin data classes from a JSON sample instantly, with nested types detected automatically. JSON to Laravel Model & Migration Generate a Laravel Eloquent model ($fillable, casts, relations) and a matching migration from a sample JSON record. JSON to Mongoose Schema Generate a Mongoose schema and model from a sample JSON document, detecting Date, ObjectId, arrays and nested objects. JSON to Objective-C Generate Objective-C classes with properties from a JSON sample instantly, with nested types detected automatically.