Skip to content

Menu

JSON to Dart

Convert JSON to Dart classes

Runs entirely in your browserConvertersFree, no sign-up
Loading tool

01What is JSON to Dart?

Writing model classes by hand is the dull part of consuming an API in Flutter. This converter reads a JSON object and generates Dart classes for it. You name the root class and pick one of two output styles. Plain Dart produces self-contained classes. Each has final fields, a constructor with named parameters marked required for non-null fields, a fromJson factory and a toJson method. Numbers are read through num and converted with toInt() or toDouble(). That way a JSON 7.0 still lands safely in an int field. Nested objects call their own fromJson, and lists of objects are mapped item by item. json_serializable mode targets the official json_serializable package instead. It emits @JsonSerializable(explicitToJson: true) classes that hand off to the generated _$ClassFromJson and _$ClassToJson functions. You get a single import and a part directive named after the root class in snake_case. explicitToJson makes nested objects serialize as maps rather than as Dart instances. Naming follows Effective Dart. snake_case, kebab-case and spaced keys become lowerCamelCase fields. Reserved words get a suffix, so class becomes classValue. So do keys that would clash with toJson, fromJson or an Object member such as hashCode. In serializable mode, @JsonKey(name: ...) keeps the original key whenever the Dart name differs. Keys are escaped inside string literals, so a key like $ref doesn't turn into string interpolation. Arrays are merged into one sample before typing. A list of objects gives one item class with every key seen in any element. Keys that are missing or null in some elements become nullable fields. Those are read with null checks in fromJson and written with ?. in toJson. Item classes get singular names, so items becomes Item and categories becomes Category. A class that would be called List, Map or String gets a numeric suffix, so it won't hide the dart:core type. A short worked example. Paste {"class":1,"items":[{"id":1},{"id":2,"qty":3}]} in Plain Dart mode. You get a Root with int classValue and List<Item> items, plus an Item class with int id and int? qty. The qty field is read as (json['qty'] as num?)?.toInt(). There are limits. The root must be a JSON object, so a bare array or primitive is rejected with an error. Null values become dynamic fields. Empty, nested or mixed arrays fall back to List<dynamic>. An empty object becomes a class with a plain ClassName() constructor. Dates stay String.

Flutter and Dart developers who need typed models, plain or json_serializable, for an API response they are wiring into an app. The tool runs entirely in your browser, so your data never leaves your device.

02How to use JSON to Dart

  1. Paste a JSON object into JSON Input. Arrays and primitives at the root are rejected.
  2. Set the Root class name.
  3. Choose Plain Dart or json_serializable.
  4. Review the Dart Output and fix any error shown in the banner.
  5. Click Copy Dart and paste the classes into your models file.

03Worked examples

Input: {"user_id":7,"price":9.99,"meta":null} (Plain Dart)
Result: class Root { final int userId; final double price; final dynamic meta; Root({ required this.userId, required this.price, this.meta, }); factory Root.fromJson(Map<String, dynamic> json) { return Root( userId: (json['user_id'] as num).toInt(), price: (json['price'] as num).toDouble(), meta: json['meta'], …
Numbers are cast through num so a JSON 7.0 still parses as int. Output is cut off after fromJson.
Input: {"class":1,"items":[{"id":1},{"id":2,"qty":3}]} (Plain Dart)
Result: class Root { final int classValue; final List<Item> items; … class Item { final int id; final int? qty; … qty: (json['qty'] as num?)?.toInt(),
class is a reserved word in Dart, so the field is renamed classValue. A key missing from some array items becomes nullable, and the list name items is singularised to Item. Excerpt only.
Input: {"user_id":7,"tags":["a"]} (json_serializable)
Result: import 'package:json_annotation/json_annotation.dart'; part 'root.g.dart'; @JsonSerializable(explicitToJson: true) class Root { @JsonKey(name: 'user_id') final int userId; final List<String> tags; Root({ required this.userId, required this.tags, }); factory Root.fromJson(Map<String, dynamic> json) => _$RootFromJson(json); Map<String, dynamic> toJson() => _$RootToJson(this); }
@JsonKey is added only for keys whose Dart name differs from the JSON key; you still need to run build_runner to generate root.g.dart.
Input: {"$ref":"x","categories":[{"name":"a"}]} (json_serializable)
Result: @JsonSerializable(explicitToJson: true) class Root { @JsonKey(name: '\$ref') final String ref; final List<Category> categories; … @JsonSerializable(explicitToJson: true) class Category { final String name;
The $ is escaped in the key literal, $ref becomes the field ref, and categories gives a singular Category class. Excerpt only.
Input: [1] (Plain Dart)
Result: Root JSON must be an object. Arrays and primitive values are not supported as root.
The error shown when the root is not an object. Wrap the array in an object, such as {"items":[...]}, to get a class.

04Common use cases

  • Creating model classes for a REST response in a Flutter app that calls the API with http or Dio
  • Setting up json_serializable models before the first build_runner run in a new Dart project
  • Typing Firebase or Supabase documents that arrive as Map<String, dynamic>
  • Turning a backend team's sample payload into typed classes during API integration
  • Replacing scattered json['field'] lookups in widgets with one typed model
  • Generating a quick model for a Dart CLI or server app built on shelf

05Tips

  • In json_serializable mode, save the file under the name in the part directive, then run dart run build_runner build.
  • Fields that were null in the sample become dynamic. Replace them with real types once you know what the API sends.
  • A key that never appears in your sample can't produce a field, so include one object where every optional field is present.
  • If an int field sometimes arrives as a decimal, change it to double or num to avoid a runtime type error.
  • With nested classes in json_serializable mode, all classes share one import and one part directive, so keep them in a single file.

06Related concepts

Dart classes, Flutter, json_serializable, build_runner, JSON deserialization, Null safety.

Frequently Asked Questions

What is JSON to Dart?

JSON to Dart is a free, browser-based developer tool on AIDToolStack. Convert JSON to Dart classes It requires no installation or sign-up and works entirely in your browser.

Is JSON to Dart free to use?

Yes. No sign-up, no fees and no usage cap.

Does JSON to Dart store my data?

No. It runs in your browser; nothing is uploaded.

Reviews and commentsShow

Reviews

Sign in to leave a review.

Loading reviews…

Comments

Sign in to leave a comment.

Loading comments…

What changed in AI dev tools, once a month

Renames, shutdowns, pricing changes and new MCP setups, each with a source.

Subscribe