Skip to content
DevPenh

JSON to Dart

Paste a JSON response to get null-safe Dart model classes with fromJson and toJson, ready for a Flutter app. Choose plain Dart with no dependencies, or annotated classes for json_serializable.

Style

Paste JSON or load the example to get started.

How types are chosen

JSON valueDart type
"text"String
42int
4.5, 1.0, 1e3double, read with (json['x'] as num).toDouble()
true / falsebool
An object with keysA new class, named after its key
{}Map<String, dynamic>
An arrayList<T>, with T taken from all of its items
null only, or mixed typesdynamic

Rules worth knowing

  • Every item of an array is used. For a list of objects, the keys of all items are merged into one class. A key that is missing from some items, or is null in any of them, becomes nullable (String?).
  • 1 and 1.5 in the same field give double. The number is read from the text you paste, so 1.0 is a double, as it is for Dart's jsonDecode.
  • Keys become lowerCamelCase fields. first_name becomes firstName, and the original key is kept in fromJson, toJson and @JsonKey. Dart keywords and names such as class, int or hashCode get a Value suffix.
  • List item classes are named in the singular. orders gives Order and categories gives Category. This is a simple English rule, so rename a class if the result reads oddly. Two classes with the same name get a number, such as Page2.
  • A root array uses its items. Pasting [{"id": 1}] generates the item class; decode it with .map(...) on the list.

Using the generated code

With the plain Dart style, no packages are needed:

import 'dart:convert';

final user = User.fromJson(jsonDecode(body) as Map<String, dynamic>);
print(user.fullName);
final back = jsonEncode(user.toJson());

With json_serializable, save the code in a file named after the part line (for example user.dart), add the packages and run the generator:

dart pub add json_annotation dev:json_serializable dev:build_runner
dart run build_runner build

In a Flutter project, use flutter pub add instead of dart pub add. The classes use explicitToJson: true, so nested objects are converted to maps by toJson.

Limitations

  • Types come from one sample. If the API sometimes sends null or leaves out a key that your sample has, the cast fails at runtime. Paste several items in an array, or tick Make all fields nullable.
  • Dates stay String; the tool does not guess DateTime or enums. Change them by hand if you want them parsed.
  • Two keys with the same structure in different places produce two classes. Merge them by hand if they are the same model.
  • freezed output is not offered yet.

The generated code was checked with dart analyze (recommended lints) and a decode-and-encode round trip on Dart 3.13.5 for a set of test payloads, in both styles.

Pasting a Flutter log

A map printed by print or debugPrint, such as {id: 1, name: Ada}, is not JSON. Use Try to repair to convert it first. Values in such logs have no quotes, so check the guessed types. The common JSON errors guide explains other parse errors, and the JSON Formatter shows their exact position.