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
UserorProduct. - 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
.dartfile 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
fromJsonandtoJson. 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.dartfile bybuild_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,toStringandtoJsonall 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 becomedouble. If a list mixes both, it usesdouble. - 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 isnullor 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 was5in your sample but5.5in real data. Paste a sample that contains a decimal, or change the field todoubleand read it with(json['price'] as num).toDouble().dynamicfields: a value that wasnullor an empty list in the sample can’t be typed. Paste a response where that field has real data, or set the type yourself.FormatExceptionwhen decoding: the response is not valid JSON, for example an HTML error page. Checkresponse.statusCodebefore callingjsonDecode, 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
Pubspec Checker
Paste your pubspec.yaml and find outdated, discontinued and new-major-version packages, with the updated file to copy.
Open toolcURL to Dart (Dio & http)
Turn a cURL command into Flutter code for Dio or http, with headers, JSON, forms, uploads and a response model.
Open toolFlutter BoxShadow Generator
Design shadows with a live preview: soft cards, Material elevation, neumorphism and glow. Copy the BoxDecoration code.
Open toolHex to Flutter Color
Convert hex, RGB or HSL to Color(0xFF…), and generate a MaterialColor swatch and Material 3 theme from one colour.
Open toolDevice Frame Mockup Generator
Put your app screenshot in an iPhone 17 Pro Max, Pixel or Galaxy frame and download a high-resolution PNG.
Open toolApp Store Screenshot Generator
Make App Store and Google Play screenshots with headlines and device frames, plus a Play Store feature graphic.
Open toolFlutter Splash Screen Generator
Preview your splash on Android 12+, older Android and iPhone, see what the Android 12 circle cuts off, and get the flutter_native_splash files.
Open toolApp Icon Generator
Upload one image and get every iOS, Android adaptive, web and Play Store icon size for your Flutter app.
Open toolCode to Image
Turn Dart, Flutter and other code into a beautiful, syntax-highlighted image for posts and slides.
Open tool