Output will appear here
Output will appear here
Paste JSON and get null-safe Dart model classes with a fromJson factory and a toJson method, ready for a Flutter app. There is no json_serializable or build_runner step: the mapping code is written out in full. Numbers are read through num, so an API that sends 5 for a double field does not crash with "int is not a subtype of double".
By Ankit Aglawe · Updated · Runs entirely in your browser
branding becomes a Branding class and each onboarding page becomes OnboardingItem. skippable is only on the second page, so it is bool? and not required in the constructor. featureFlags is decoded with .map((e) => e as String).toList(), which gives a real List<String> rather than a List<dynamic>.
{
"appName": "Fieldnotes",
"minSupportedBuild": 412,
"maintenanceMode": false,
"branding": { "seedColor": "#2E7D32", "useMaterial3": true, "fontScale": 1.1 },
"featureFlags": ["offline_sync", "dark_mode"],
"onboarding": [
{ "title": "Capture", "image": "assets/onboard_1.png" },
{ "title": "Sync", "image": "assets/onboard_2.png", "skippable": true }
]
}class Branding {
final String seedColor;
final bool useMaterial3;
final double fontScale;
Branding({
required this.seedColor,
required this.useMaterial3,
required this.fontScale,
});
factory Branding.fromJson(Map<String, dynamic> json) {
return Branding(
seedColor: json['seedColor'] as String,
useMaterial3: json['useMaterial3'] as bool,
fontScale: (json['fontScale'] as num).toDouble(),
);
}
Map<String, dynamic> toJson() {
return <String, dynamic>{
'seedColor': seedColor,
'useMaterial3': useMaterial3,
'fontScale': fontScale,
};
}
}
class OnboardingItem {
final String title;
final String image;
final bool? skippable;
OnboardingItem({
required this.title,
required this.image,
this.skippable,
});
factory OnboardingItem.fromJson(Map<String, dynamic> json) {
return OnboardingItem(
title: json['title'] as String,
image: json['image'] as String,
skippable: json['skippable'] == null ? null : json['skippable'] as bool,
);
}
Map<String, dynamic> toJson() {
return <String, dynamic>{
'title': title,
'image': image,
'skippable': skippable,
};
}
}
class Root {
final String appName;
final int minSupportedBuild;
final bool maintenanceMode;
final Branding branding;
final List<String> featureFlags;
final List<OnboardingItem> onboarding;
Root({
required this.appName,
required this.minSupportedBuild,
required this.maintenanceMode,
required this.branding,
required this.featureFlags,
required this.onboarding,
});
factory Root.fromJson(Map<String, dynamic> json) {
return Root(
appName: json['appName'] as String,
minSupportedBuild: (json['minSupportedBuild'] as num).toInt(),
maintenanceMode: json['maintenanceMode'] as bool,
branding: Branding.fromJson(json['branding'] as Map<String, dynamic>),
featureFlags: (json['featureFlags'] as List<dynamic>).map((e) => e as String).toList(),
onboarding: (json['onboarding'] as List<dynamic>).map((e) => OnboardingItem.fromJson(e as Map<String, dynamic>)).toList(),
);
}
Map<String, dynamic> toJson() {
return <String, dynamic>{
'appName': appName,
'minSupportedBuild': minSupportedBuild,
'maintenanceMode': maintenanceMode,
'branding': branding.toJson(),
'featureFlags': featureFlags,
'onboarding': onboarding.map((e) => e.toJson()).toList(),
};
}
}A top-level array produces the item class plus typedef RootArray = List<Root>;. Nullable fields get a null check in fromJson, and the optional attachment is encoded with attachment?.toJson(). Timestamps stay String.
[
{ "id": 88123, "senderId": "u_42", "text": "Running 5 min late", "sentAt": "2024-11-02T18:03:11Z", "readAt": null },
{ "id": 88124, "senderId": "u_7", "text": "No worries", "sentAt": "2024-11-02T18:03:40Z", "readAt": "2024-11-02T18:04:02Z", "replyTo": 88123 },
{ "id": 88125, "senderId": "u_42", "text": null, "sentAt": "2024-11-02T18:05:00Z", "readAt": null, "attachment": { "url": "https://cdn.example.com/p/9.jpg", "width": 1080, "height": 1350 } }
]class Attachment {
final String url;
final int width;
final int height;
Attachment({
required this.url,
required this.width,
required this.height,
});
factory Attachment.fromJson(Map<String, dynamic> json) {
return Attachment(
url: json['url'] as String,
width: (json['width'] as num).toInt(),
height: (json['height'] as num).toInt(),
);
}
Map<String, dynamic> toJson() {
return <String, dynamic>{
'url': url,
'width': width,
'height': height,
};
}
}
class Root {
final int id;
final String senderId;
final String? text;
final String sentAt;
final String? readAt;
final int? replyTo;
final Attachment? attachment;
Root({
required this.id,
required this.senderId,
this.text,
required this.sentAt,
this.readAt,
this.replyTo,
this.attachment,
});
factory Root.fromJson(Map<String, dynamic> json) {
return Root(
id: (json['id'] as num).toInt(),
senderId: json['senderId'] as String,
text: json['text'] == null ? null : json['text'] as String,
sentAt: json['sentAt'] as String,
readAt: json['readAt'] == null ? null : json['readAt'] as String,
replyTo: json['replyTo'] == null ? null : (json['replyTo'] as num).toInt(),
attachment: json['attachment'] == null ? null : Attachment.fromJson(json['attachment'] as Map<String, dynamic>),
);
}
Map<String, dynamic> toJson() {
return <String, dynamic>{
'id': id,
'senderId': senderId,
'text': text,
'sentAt': sentAt,
'readAt': readAt,
'replyTo': replyTo,
'attachment': attachment?.toJson(),
};
}
}
typedef RootArray = List<Root>;Every code generator on this site is run on the same awkward input: a 64-bit id, a float written as 1.0, a null, a date string, a reserved word as a key, an empty array, a nested array and an array of objects whose keys differ. This is the exact output for Dart, generated when the page was built.
{
"id": 9007199254740993,
"price": 19.99,
"ratio": 1.0,
"deleted_at": null,
"created_at": "2024-01-15T10:30:00Z",
"class": "premium",
"tags": [],
"matrix": [[1, 2], [3, 4.5]],
"items": [{ "sku": "A1", "qty": 1 }, { "sku": "B2", "qty": 2, "note": "gift" }]
}class Item {
final String sku;
final int qty;
final String? note;
Item({
required this.sku,
required this.qty,
this.note,
});
factory Item.fromJson(Map<String, dynamic> json) {
return Item(
sku: json['sku'] as String,
qty: (json['qty'] as num).toInt(),
note: json['note'] == null ? null : json['note'] as String,
);
}
Map<String, dynamic> toJson() {
return <String, dynamic>{
'sku': sku,
'qty': qty,
'note': note,
};
}
}
class Root {
final int id;
final double price;
final int ratio;
final dynamic deletedAt;
final String createdAt;
final String class_;
final List<dynamic> tags;
final List<List<double>> matrix;
final List<Item> items;
Root({
required this.id,
required this.price,
required this.ratio,
this.deletedAt,
required this.createdAt,
required this.class_,
required this.tags,
required this.matrix,
required this.items,
});
factory Root.fromJson(Map<String, dynamic> json) {
return Root(
id: (json['id'] as num).toInt(),
price: (json['price'] as num).toDouble(),
ratio: (json['ratio'] as num).toInt(),
deletedAt: json['deleted_at'],
createdAt: json['created_at'] as String,
class_: json['class'] as String,
tags: json['tags'] as List<dynamic>,
matrix: (json['matrix'] as List<dynamic>).map((e) => (e as List<dynamic>).map((e) => (e as num).toDouble()).toList()).toList(),
items: (json['items'] as List<dynamic>).map((e) => Item.fromJson(e as Map<String, dynamic>)).toList(),
);
}
Map<String, dynamic> toJson() {
return <String, dynamic>{
'id': id,
'price': price,
'ratio': ratio,
'deleted_at': deletedAt,
'created_at': createdAt,
'class': class_,
'tags': tags,
'matrix': matrix,
'items': items.map((e) => e.toJson()).toList(),
};
}
}A field that is null in some samples becomes String? or int?, and fromJson checks for null before casting. A field that is null in every sample, like deleted_at, becomes dynamic, since there is nothing to infer a type from.
Whole numbers become int, decoded with (json[key] as num).toInt(). Decimals become double, decoded with .toDouble(). Because both go through num, a server that sends 1.0 for an int field, or 19 for a double field, still parses. Note that ratio: 1.0 is typed int here, because the browser reads 1.0 as 1.
On Android, iOS and desktop, Dart int is 64-bit and jsonDecode keeps 9007199254740993 exact. On Flutter web, int is a JavaScript number and loses precision above 2^53. If you target web, have the API send big ids as strings.
A key that is missing from some objects becomes nullable and drops required from the constructor. toJson still writes it, so a missing note is sent back as "note": null rather than left out. Filter nulls in toJson if your API rejects them.
Dart has no way to escape an identifier, so a keyword key gets an underscore: class becomes class_. The JSON key is unchanged inside fromJson and toJson. Other keys are camelCased, so deleted_at becomes deletedAt.
ISO 8601 strings stay String. Call DateTime.parse(createdAt) where you need a date, or change the field type and the fromJson line together.
An empty array becomes List<dynamic>. Nested arrays are decoded level by level, so matrix becomes List<List<double>> with a nested .map(...).toList(). Arrays of objects call the item class fromJson for each element.
| JSON value | Dart type | fromJson decoding |
|---|---|---|
| string | String | as String |
| whole number | int | (x as num).toInt() |
| number with a fraction | double | (x as num).toDouble() |
| true / false | bool | as bool |
| null in some samples | T? | x == null ? null : ... |
| always null | dynamic | passed through |
| key missing in some objects | T? | not required |
| object | class Name | Name.fromJson(x as Map<String, dynamic>) |
| array | List<T> | .map(...).toList() |
| empty array or mixed types | List<dynamic> / dynamic |
Enter a JSON object in the input editor. The converter supports nested objects, arrays, and nullable fields commonly found in REST API responses.
The output pane shows Dart classes with factory fromJson() constructors and toJson() methods, ready for integration with Flutter networking code.
Copy the classes or download a .dart file. Drop them into your lib/models directory and use them with http or Dio for API response parsing.
Paste a sample API response, copy the classes into a file such as models.dart, then call Root.fromJson(jsonDecode(response.body) as Map<String, dynamic>). jsonDecode comes from dart:convert.
No. The fromJson and toJson code is written out by hand, so there are no annotations, no part files and no build_runner to run. You can still move to json_serializable later; the field names and types carry over.
For a top-level array, decode it and map each item: (jsonDecode(body) as List).map((e) => Root.fromJson(e as Map<String, dynamic>)).toList(). The output also includes a RootArray typedef for that list.
Yes. Fields are non-nullable unless the sample showed a null or a missing key, in which case they are T? and optional in the constructor.
That error comes from casting a JSON number straight to double. The generated code casts to num first and calls .toDouble(), so whole numbers in a double field parse fine.
Nested classes are named after their key, so a theme or icon object becomes Theme or Icon, which hides the Material class of the same name. Rename the class, or import your models with a prefix.
Need several formats at once? Convert All shows every output from one paste. Or browse all JSON converters.