JSON → Dart Model

Class:
JSON Input
Loading editor…
Dart Classes
Loading editor…

JSON to Dart Model Generator

Paste an API response and get null-safe Dart model classes for your Flutter app. Choose plain classes with hand-written fromJson / toJson, json_serializable annotations, or immutable Freezed models. Nested objects become their own classes, arrays become typed List<T>, and ISO dates become DateTime.

Null safety
Fields that can be null or missing get a ?.
3 output styles
Manual, json_serializable, or Freezed.
Nested classes
Deep objects and lists of objects are fully typed.
Client-side
No data is uploaded. Everything runs in your browser.

How JSON Maps to Dart Classes

Every JSON object becomes a Dart class with final fields and a const constructor using named parameters. Fields present in every sample are required; fields that are null or missing somewhere become nullable and optional.

Input JSON:

{
  "id": 1,
  "name": "Alice",
  "score": 9.5,
  "created_at": "2024-05-01T10:30:00Z",
  "tags": ["admin", "editor"],
  "avatar": null
}

Generated Dart (Manual):

class Root {
  final int id;
  final String name;
  final double score;
  final DateTime createdAt;
  final List<String> tags;
  final dynamic avatar;

  const Root({
    required this.id,
    required this.name,
    required this.score,
    required this.createdAt,
    required this.tags,
    this.avatar,
  });

  factory Root.fromJson(Map<String, dynamic> json) {
    return Root(
      id: json['id'] as int,
      name: json['name'] as String,
      score: (json['score'] as num).toDouble(),
      createdAt: DateTime.parse(json['created_at'] as String),
      tags: (json['tags'] as List<dynamic>).map((e) => e as String).toList(),
      avatar: json['avatar'],
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'name': name,
      'score': score,
      'created_at': createdAt.toIso8601String(),
      'tags': tags,
      'avatar': avatar,
    };
  }
}

Use it with Root.fromJson(jsonDecode(response.body)) and jsonEncode(root.toJson()) from dart:convert.

Manual vs json_serializable vs Freezed

Manual writes fromJson and toJson by hand — no packages, no code generation, and nothing hidden. Best for small apps or when you want full control.

json_serializable adds @JsonSerializable(explicitToJson: true) and delegates to generated _$ClassFromJson / _$ClassToJson functions. Add json_annotation to dependencies and json_serializable + build_runner to dev dependencies, then run dart run build_runner build.

Freezed generates immutable classes with copyWith, value equality, toString and JSON support. It uses the Freezed 3 syntax (abstract class X with _$X). Add freezed_annotation and json_annotation to dependencies, freezed, json_serializable and build_runner to dev dependencies, and run build_runner.

In the generated modes, keys that don't match the Dart field name get @JsonKey(name: 'original_key'), so created_at maps to createdAt correctly.

Types, Numbers and Null Safety

Whole numbers become int and decimals become double. If a field is a whole number in one sample and a decimal in another, it's typed double. Doubles are read with (json['x'] as num).toDouble(), because JSON like 10 decodes to an int in Dart and a plain as double cast would throw at runtime.

ISO 8601 strings become DateTime (parsed with DateTime.parse, written back with toIso8601String()). A field that's only ever null, or whose type varies, is typed dynamic.

Turn on All nullable to make every field nullable and optional. It's the safest choice when an API may omit fields your sample happened to include.

Frequently Asked Questions

How do I parse JSON into a Dart model in Flutter?

Decode the response with jsonDecode from dart:convert, then pass the map to the generated factory: final user = User.fromJson(jsonDecode(response.body) as Map<String, dynamic>);. For a JSON array, map each element: (jsonDecode(body) as List).map((e) => User.fromJson(e)).toList().

Is the generated code null-safe?

Yes. All output targets sound null safety (Dart 2.12+ and Dart 3). Fields that are null or missing in your sample are nullable (String?) and optional in the constructor; everything else is non-nullable and required.

Should I use json_serializable or Freezed?

Use json_serializable if you just need JSON conversion. Use Freezed if you also want immutable models with copyWith, value equality and union types — it builds on json_serializable for the JSON part. Both need build_runner.

Why does a double field use `as num` instead of `as double`?

Dart's JSON decoder turns 10 into an int and 10.5 into a double. A plain as double cast throws when the API sends a whole number, so the generator reads the value as num and converts it with .toDouble().

What does the json_serializable `explicitToJson: true` option do?

It makes the generated toJson call toJson() on nested objects too, so you get a plain Map tree all the way down. Without it, nested objects are left as class instances inside the map.

Is my JSON uploaded anywhere?

No. The entire conversion runs in your browser with no network requests. Nothing is transmitted.