Youtilities

Popular

All tools

JSON to Dart Class

Generate null-safe Dart classes with fromJson and toJson from any JSON. Made for Flutter.

Runs in your browser. Nothing is uploaded.

Input

Waiting for JSON

Dart
Dart types appear here.

What this does

Paste JSON and get null-safe Dart classes with fromJson and toJson, ready for Flutter. Nested objects become their own classes; arrays of objects are merged into one class.

Null safety

A key that is missing from some items, or sometimes null, becomes a nullable field (String?) and is optional in the constructor; everything else is required. Whole numbers are int, decimals double, unknown values dynamic.

Names

Fields are camelCase; fromJson and toJson use the original keys, so first-name reads and writes correctly as firstName. Two objects with the same shape share one class.

Using it in Flutter

Decode with final root = Root.fromJson(jsonDecode(response.body) as Map<String, dynamic>), and encode back with jsonEncode(root.toJson()). Everything is plain Dart, so there is no build_runner step and no generated .g.dart file to keep in sync. Fields are final and the constructor is const, so objects are immutable; add a copyWith method by hand if you need edited copies.

Why doubles use num

jsonDecode returns an int for 5 and a double for 5.5, and a cast like json['price'] as double throws when the API sends a whole number. The generated code reads decimal fields as (json['price'] as num).toDouble(), so both forms work. Lists of plain values use .cast<String>() and lists of objects map each item through fromJson.

Fixing type errors

type 'Null' is not a subtype of type 'String' means a key the sample always had is missing or null in real data. Mark that field nullable, or turn on All optional and generate again. Dart keywords used as keys, such as class or default, get a trailing underscore, while toJson still writes the original key.

All json tools