Free tool

JSON to Dart converter

Paste your JSON and get ready-to-use, null-safe Dart model classes for your Flutter app, as plain Dart, json_serializable or freezed. Nested objects and lists become their own classes.

Dart model

class Shop {
  const Shop({
    required this.id,
    required this.name,
    required this.rating,
    required this.isOpen,
    required this.tags,
    required this.address,
    required this.menu,
  });

  final int id;
  final String name;
  final double rating;
  final bool isOpen;
  final List<String> tags;
  final Address address;
  final List<MenuItem> menu;

  factory Shop.fromJson(Map<String, dynamic> json) {
    return Shop(
      id: json['id'] as int,
      name: json['name'] as String,
      rating: (json['rating'] as num).toDouble(),
      isOpen: json['isOpen'] as bool,
      tags: (json['tags'] as List<dynamic>).map((e) => e as String).toList(),
      address: Address.fromJson(json['address'] as Map<String, dynamic>),
      menu: (json['menu'] as List<dynamic>).map((e) => MenuItem.fromJson(e as Map<String, dynamic>)).toList(),
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'name': name,
      'rating': rating,
      'isOpen': isOpen,
      'tags': tags,
      'address': address.toJson(),
      'menu': menu.map((e) => e.toJson()).toList(),
    };
  }
}

class Address {
  const Address({
    required this.city,
    required this.postcode,
  });

  final String city;
  final String postcode;

  factory Address.fromJson(Map<String, dynamic> json) {
    return Address(
      city: json['city'] as String,
      postcode: json['postcode'] as String,
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'city': city,
      'postcode': postcode,
    };
  }
}

class MenuItem {
  const MenuItem({
    required this.id,
    required this.title,
    required this.price,
    this.isNew,
  });

  final int id;
  final String title;
  final double price;
  final bool? isNew;

  factory MenuItem.fromJson(Map<String, dynamic> json) {
    return MenuItem(
      id: json['id'] as int,
      title: json['title'] as String,
      price: (json['price'] as num).toDouble(),
      isNew: json['isNew'] as bool?,
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'title': title,
      'price': price,
      'isNew': isNew,
    };
  }
}

Your JSON never leaves your device: the conversion runs entirely in your browser.

Why I built this

For small models I still write Dart classes by hand. But when an API sends back a big response with lots of fields, typing the model yourself almost always leads to mistakes: a misspelled key, a wrong field name or the wrong type, and you only find out when the app throws an error. This converter writes that model for you from a real response. If you want to understand what the generated code does line by line, read my guide on parsing JSON into Dart model classes.

How to use it

  • Paste a JSON response from your API into the left box, or edit the example.
  • Pick an output style: Plain Dart, json_serializable or freezed (see the comparison below).
  • Set the class name for the top-level object, for example User or Product.
  • Choose options: toJson() to send data back to an API, copyWith() for immutable state updates, and All fields nullable if your API sometimes leaves fields out.
  • Copy the code or download it as a .dart file and add it to your project.

Using the model in Flutter

Decode the response with jsonDecode and pass it to the generated fromJson factory:

import 'dart:convert';
import 'package:http/http.dart' as http;

Future<Shop> fetchShop() async {
  final response = await http.get(Uri.parse('https://api.example.com/shop/42'));
  if (response.statusCode != 200) throw Exception('Failed to load shop');
  return Shop.fromJson(jsonDecode(response.body) as Map<String, dynamic>);
}

Plain Dart vs json_serializable vs freezed

All three styles read and write exactly the same JSON. I tested the output of each one with the same sample: they decode it, encode it back, and produce identical JSON. The difference is how much code you write and what you get for free.

  • Plain Dart: everything is in one file with hand-written fromJson and toJson. No packages and no build step, so it is the easiest to read and the best choice for beginners and small apps.
  • json_serializable: the classes are annotated with @JsonSerializable() and the parsing code is generated into a .g.dart file by build_runner. Keys that are not valid Dart names get @JsonKey(name: 'user_name'). Good for big models that change often: you edit the fields and regenerate.
  • freezed: immutable classes with copyWith, ==, hashCode, toString and toJson all generated. This is the most common choice with Riverpod and Bloc, where state must never be changed in place. The output uses the Freezed 3 syntax (abstract class ... with _$Name).

With the two generated styles, save the file under the name the tool shows (for example lib/shop.dart), because the part 'shop.g.dart'; line must match the file name. Then run dart run build_runner build --delete-conflicting-outputs. If you see Target of URI hasn’t been generated, the build has not run yet or the file name does not match.

How types are detected

  • Whole numbers become int, decimals become double. If a list mixes both, it uses double.
  • Nested objects become their own class, named after the key (address → Address).
  • Lists of objects get a singular class name (categories → List<Category>). All items are merged, so a field missing from some items becomes nullable.
  • null values and empty lists become dynamic, because their real type can’t be known from the sample.
  • Keys are converted to camelCase field names (user_name → userName), and Dart keywords get a suffix (class → classValue).

Common JSON errors in Flutter, and how to avoid them

The generated model is only as good as the sample you paste. These are the runtime errors that most often come from a sample that doesn’t show the full picture:

  • type 'Null' is not a subtype of type 'String': a field that had a value in your sample is null or missing in another response. This is the error I have hit most myself. Turn on All fields nullable, or make just that field nullable in the generated class.
  • type 'int' is not a subtype of type 'double' (or the other way round): a price or rating was 5 in your sample but 5.5 in real data. Paste a sample that contains a decimal, or change the field to double and read it with (json['price'] as num).toDouble().
  • dynamic fields: a value that was null or an empty list in the sample can’t be typed. Paste a response where that field has real data, or set the type yourself.
  • FormatException when decoding: the response is not valid JSON, for example an HTML error page. Check response.statusCode before calling jsonDecode, as in the example above.

Tip: if you can, paste two or three items of a list instead of one. All items are merged, so fields that only some items have become nullable automatically.

FAQ

Does it support null safety? Yes. The generated code targets Dart 3 with sound null safety and passes dart analyze with no issues.

Do I need json_serializable or build_runner? Only if you pick those styles. The default Plain Dart output has hand-written fromJson and toJson, so there is no code generation step and no extra packages.

Why does freezed output start with // ignore_for_file: invalid_annotation_target? When a model has nested classes, the tool adds @JsonSerializable(explicitToJson: true) to the factory so toJson() also converts the nested objects. That is how the freezed docs configure it, and the analyzer shows a warning for it unless that line is there.

Fetching the JSON with a cURL command from your API docs? The cURL to Dart converter turns it into Dio or http code and builds the model at the same time.

Is my data safe? Yes. Nothing is uploaded: the JSON is converted by JavaScript in your browser.

Want to see these models in a real app? Browse my Flutter projects with full source code.

More free Flutter tools