TXT · Text & Data tools

JSON to Dart Class Converter

Top-level type
Part directive file
JSON input
Dart output
Inference
DartlanguageJSON sampleinput0types0lines out0chars out
Go to the full JSON to Types generator with all ten languages

Shareable link

Settings are written to the URL as you change them. Nothing differs from the defaults yet.

Dart classes from a JSON sample

Paste a JSON document and get Dart model classes for it. The default output needs no build step: each class carries its own fromJson factory and toJson method that read and write a Map<String, dynamic>. For projects already on build_runner, two checkboxes switch to json_serializable or freezed annotations instead.

Example

class Order {
    Order({
        required this.sku,
        required this.qty,
        this.gift,
    });

    final String sku;
    final int qty;
    final bool? gift;

    factory Order.fromJson(Map<String, dynamic> json) => Order(
        sku: json["sku"],
        qty: json["qty"],
        gift: json["gift"],
    );

    Map<String, dynamic> toJson() => {
        "sku": sku,
        "qty": qty,
        "gift": gift,
    };
}

gift is bool? and not required because only some orders in the sample carry it.

Code generation paths

json_serializable annotations adds @JsonSerializable() to each class and rewrites fromJson to call the generated _$OrderFromJson. Set part file so the part 'models.g.dart'; line matches your file name. freezed writes the class as an @freezed abstract class with a const factory constructor; it pairs with json_serializable annotations for JSON support. copyWith() adds a copyWith method to the hand-written path; freezed generates its own.

Limits

Dart has no union type, so a field that mixes types in the sample becomes dynamic. Repeated string values become an enum with a value map when enum inference is on. Dates become DateTime with DateTime.parse in fromJson when date inference is on.

Frequently Asked Questions

One class per object shape with final fields, a constructor, and factory fromJson and toJson methods written by hand against Map<String, dynamic>, so no code generation step is needed.

Yes. Check json_serializable annotations to emit @JsonSerializable() classes with fromJson delegating to the generated _$XFromJson, and set part file to the name your part directive should carry.

It writes each class as an @freezed abstract class with a const factory, the shape the freezed code generator expects. Combine it with json_serializable annotations for fromJson support.

A key missing from some objects becomes a nullable type such as bool?, and the constructor parameter is not required. Check All properties required to make every parameter required regardless.

Yes. copyWith() generates a method with one optional parameter per field; passing nothing keeps the current value. For a field that should be reset to null, prefer freezed, which handles that case explicitly.

Generate types for one language

Full JSON to Types Generator tool

Explore Our Tools

Browse all tools