JSON to Dart (Flutter)

Convert JSON to null-safe Dart model classes for Flutter, with fromJson, toJson and an optional copyWith. Free, no sign-up, runs in your browser.

Runs in your browser. Your data never leaves your device.

How to use the JSON to Dart (Flutter)

  1. Paste a sample JSON response into the box.
  2. Set the root class name and tick the options: all fields nullable, copyWith or final fields.
  3. Copy the classes into a .dart file, for example under lib/models.

About this tool

Paste a JSON response and get Dart classes ready for a Flutter app. Each object becomes a class with a fromJson factory constructor and a toJson method, nested objects and lists are converted for you, and the code follows Dart's sound null safety: a value that is null or missing in your sample becomes a nullable type such as String?. The classes are plain Dart, so they need no extra package and no build step.

Using the model in Flutter

Decode the response with jsonDecode from dart:convert and pass the map to fromJson. To send data back, call toJson and encode the result with jsonEncode.

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

Future<User> fetchUser() async {
  final res = await http.get(Uri.parse('https://api.example.com/users/1'));
  if (res.statusCode != 200) throw Exception('HTTP ${res.statusCode}');
  return User.fromJson(jsonDecode(res.body) as Map<String, dynamic>);
}

// Dart object back to JSON
final body = jsonEncode(user.toJson());

Null safety

Every Dart 3 project runs with sound null safety, so a model has to say which fields can be null. The converter marks a field nullable when it is null or missing in any item of your sample. If your API isn't consistent, tick All fields nullable and check the values where you use them. Code from converters that ignore null safety won't compile in a current Flutter project.

freezed, json_serializable and quicktype

freezed and json_serializable generate fromJson and toJson with build_runner, and freezed adds copyWith, equality and union types. They suit large apps with many models, at the cost of extra packages, annotations and a build step. quicktype is an open source generator for many languages. The classes made here are plain Dart: paste them in and they work. Tick copyWith if you update models in your state management.

Model or entity?

In clean architecture the class made from JSON is the model in the data layer, and the entity is the plain class your app logic uses. Many apps use the model directly. If you keep both, generate the model here and map it to the entity in your repository. VS Code and Android Studio also have extensions that make Dart classes from JSON inside the editor.

GuideHow to Make an App and Earn Money (Step by Step Guide)

Frequently asked questions

What is the best JSON to Dart converter?

One that writes null-safe code which compiles on current Flutter without extra packages. This converter does that and adds copyWith when you ask. For large apps that also want generated equality and union types, use freezed.

Does the code support null safety?

Yes. Fields that are null or missing in your sample get a nullable type, and you can make every field nullable with one tick.

Do I need freezed or json_serializable?

No. The classes include their own fromJson and toJson. Use freezed or json_serializable only if your project already uses build_runner.

Can I paste a JSON array?

Yes. The converter reads every item of the array, so a field that appears in only some items becomes nullable.

Is my JSON sent anywhere?

No. Everything runs in your browser.

More json tools

JSON and code models

All 31 free tools →
JSON tool · Runs in your browser

JSON Formatter

Format JSON online: beautify messy JSON with 2 or 4 spaces or tabs, sort keys, minify it to one line and find errors. Free, runs in your browser.

Free · No sign-upOpen tool
JSON tool · Runs in your browser

JSON Validator

Check if JSON is valid and see the exact line and column of any error, with a hint on how to fix it. A free online JSON validator in your browser.

Free · No sign-upOpen tool
JSON tool · Runs in your browser

JSON to Kotlin Data Class

Paste JSON and get Kotlin data classes for Android, with kotlinx.serialization, Gson or Moshi names, nullable fields and defaults. Free, in your browser.

Free · No sign-upOpen tool
JSON tool · Runs in your browser

JSON to Swift (Codable)

Turn JSON into Swift Codable structs or classes, with CodingKeys for snake_case keys. Free JSON to Swift converter for iOS and macOS apps.

Free · No sign-upOpen tool