JSON to Dart Class

Generate Dart classes with fromJson/toJson for Flutter and server-side Dart. No packages required.

Generates fromJson / toJson methods. Paste output directly into your .dart file.

What is Dart JSON Serialization?

Dart does not have built-in JSON serialization like some languages. Instead, you write fromJson factory constructors and toJson methods in your model classes to convert between Map<String, dynamic> (what jsonDecode() returns) and your typed Dart objects.

In Flutter apps, this pattern is used to parse HTTP responses from REST APIs, decode data from shared preferences or local storage, and deserialize notifications or WebSocket messages. Writing these classes by hand for every API response is repetitive — this tool generates the complete class hierarchy from your JSON instantly, with no code generation step required.

JSON to Dart Class Example

Input JSON:

json
{
  "id": 1,
  "name": "Alice",
  "email": "alice@example.com",
  "address": { "city": "Berlin", "zip": "10115" },
  "tags": ["flutter", "dart"]
}

Generated Dart class:

dart
class Address {
  final String? city;
  final String? zip;

  Address({
    this.city,
    this.zip,
  });

  factory Address.fromJson(Map<String, dynamic> json) => Address(
    city: json['city'] as String?,
    zip: json['zip'] as String?,
  );

  Map<String, dynamic> toJson() => {
    'city': city,
    'zip': zip,
  };
}

class Root {
  final int? id;
  final String? name;
  final String? email;
  final Address? address;
  final List<dynamic>? tags;

  Root({
    this.id,
    this.name,
    this.email,
    this.address,
    this.tags,
  });

  factory Root.fromJson(Map<String, dynamic> json) => Root(
    id: json['id'] as int?,
    name: json['name'] as String?,
    email: json['email'] as String?,
    address: json['address'] != null
        ? Address.fromJson(json['address'] as Map<String, dynamic>)
        : null,
    tags: json['tags'],
  );

  Map<String, dynamic> toJson() => {
    'id': id,
    'name': name,
    'email': email,
    'address': address?.toJson(),
    'tags': tags,
  };
}

Use with Dio (Flutter)

dart
final dio = Dio();
final response = await dio.get('https://api.example.com/user/1');
final user = Root.fromJson(response.data as Map<String, dynamic>);
print(user.name);

Type Mapping

JSON TypeDart TypeNotes
stringString?Nullable for safety
integerint?Dart int (platform native)
floatdouble?Dart double
booleanbool?Nullable
nulldynamicMost permissive type
arrayList<T>?Nullable list
objectclassNew named Dart class

Frequently Asked Questions

No — the generated code uses hand-written fromJson/toJson methods with no external packages or build_runner required.

Use jsonDecode(response.body) to get a Map<String, dynamic>, then pass it to Root.fromJson(). No extra config needed.

API responses often omit fields. Making all fields nullable prevents runtime errors when a field is missing. Remove the ? for required fields.

Yes — the Dart classes are plain objects. Use them in StateNotifier, Cubit, or any state management solution.

Not directly — the generator produces manual fromJson/toJson without annotations. This avoids the build_runner step. Add @JsonSerializable() manually if you prefer code generation.

Related Tools