JSON to Dart Class

Generate Dart classes from a JSON sample.

Open tool

Overview

Paste a JSON sample and the generator produces Dart classes with appropriate fields, types, fromJson/toJson constructors, and nested types for sub-objects and arrays. The output is ready to drop into a Flutter project or any other Dart codebase.

It's for Flutter and Dart developers consuming JSON APIs without using code generation tools like json_serializable or freezed. Reach for it when scaffolding a model from a real API response, prototyping a quick integration, or reverse-engineering an unknown payload.

How it works

The generator parses the JSON sample, walks it recursively, and builds a class definition per object - field names become Dart identifiers (camelCase, with snake_case originals preserved in fromJson mappings), types inferred from the value shape: String, int, double, bool, List<T>, or a nested class. Null values widen the field to T?.

Generated classes follow Dart 3 idioms: const constructors where possible, nullable types via ? (sound null safety), and a Map<String, dynamic> round-trip pair so the model can be re-serialised. Heterogeneous arrays fall back to List<dynamic> with a comment flagging the inference.

Examples

  • Input:
    {"id": 1, "name": "Alex", "active": true}
    
    Output:
    class User {
      final int id;
      final String name;
      final bool active;
      const User({required this.id, required this.name, required this.active});
    
      factory User.fromJson(Map<String, dynamic> json) => User(
        id: json['id'] as int,
        name: json['name'] as String,
        active: json['active'] as bool,
      );
    
      Map<String, dynamic> toJson() => {'id': id, 'name': name, 'active': active};
    }
    
  • Nested object becomes a separate class.
  • Array of objects -> List<Item>.
  • A null field becomes nullable: String? note.

FAQ

Does it use json_serializable annotations?

No - generated classes have hand-written fromJson/toJson so they work without code generation. Swap in annotations if you prefer the generated approach.

What about freezed?

This generator outputs plain classes. For freezed (sealed unions, copyWith, equality) generate plain classes first, then convert manually or via a freezed migration.

How does it handle dates?

ISO 8601 strings remain String - Dart's DateTime.parse works on them. Add a typed DateTime field if you need richer semantics.

Can I rename classes?

The top-level class is named from the JSON's outer key context; nested classes inherit names from their parent property (UserAddress, UserPreferences). Rename freely after generation.

Try JSON to Dart Class

An unhandled error has occurred. Reload ×