JSON to Dart Converter

Null-safe Dart model classes for Flutter and Dart apps

JSON Input

Dart Output

Paste your JSON on the left and the Dart code shows up here.

Or drop a file on this box, , or .

What does the JSON to Dart converter generate?

Any Flutter screen that calls an API needs model classes, and hand-writing fromJson for a three-level response is where the afternoon goes. Paste one real response and you get a Dart class per JSON object: final fields, a constructor built from required this.field, and sound null safety respected, so with Nullable Types on, a stop's optional note becomes String? and drops out of the required list.

With the json_serializable box ticked, the classes are annotated for json_serializable: one import and one part 'root_object.g.dart'; at the top, @JsonSerializable(explicitToJson: true) on each class, and @JsonKey(name: ...) wherever the Dart name differs from the key. Run build_runner to create the .g.dart file. Untick it and you get plain Dart instead: a factory fromJson and a toJson() written out in full, with casts like (json['total'] as num).toDouble() and lists mapped element by element. That version works with nothing but jsonDecode.

Types come from every value in the sample, not just the first array element, so mixed arrays and keys that only some objects have are handled. Integers are int (64-bit on the Dart VM, but only exact up to 2^53 when compiled to JavaScript) and anything bigger becomes num. Keys that are Dart keywords get a trailing underscore (class_), and keys starting with a digit get an n prefix, since a leading underscore would make the field library-private. It all runs in your browser.

How to use it

The output follows your typing, so just paste and look.

1

Paste a response

Paste a response into JSON Input, pull a saved one in with Upload, or let Sample load a subscriber with a nested account and devices. A ride-hailing trip covers the awkward cases, a rating nobody has given yet and a stop with an extra field:

{"tripId":"TR-5520","driver":"Sigrid Lund","distanceKm":14.2,"fareCents":2350,"rating":null,"stops":[{"lat":59.33,"lng":18.06},{"lat":59.34,"lng":18.07,"note":"gate code 4410"}]}

Pick the root class name in Class Name. It also sets the file name (RootObject becomes root_object.dart), which matters because the part directive has to match it. Not sure the JSON is valid? Run it through the JSON Validator first.

2

Choose json_serializable or plain Dart

The Dart Output panel shows the root class first and its nested classes below it. Toggle json_serializable and Nullable Types to see how the code changes.

3

Copy or download

Copy puts the code on your clipboard; Download saves it under the snake_case file name. In json_serializable mode, add json_annotation and json_serializable to your pubspec and run dart run build_runner build.

Where this saves time

Flutter API models

Paste the response from your orders endpoint and get Order, Customer and LineItem classes ready for Order.fromJson(jsonDecode(response.body)).

Skipping build_runner on small projects

For a prototype or a package that shouldn't depend on code generation, untick json_serializable and keep the hand-written fromJson/toJson. The Flutter JSON guide covers both approaches.

Firebase and local JSON fixtures

Export a document from Firestore or a fixture from your tests and turn it into a typed model, so widgets read user.displayName instead of data['displayName'].

Spotting nullable fields

Paste a few real records into one array. Any field that is null or absent in one of them comes out nullable, which shows you where the API isn't as strict as its docs claim.

Common Questions

Do I need json_serializable?

No. With the box unticked the output has complete fromJson and toJson methods and needs no packages. With it ticked you need json_annotation, json_serializable and build_runner, and the methods are generated into root_object.g.dart.

Why is a field called class_ or n2fa?

A JSON key can be anything, but a Dart field can't be a reserved word or start with a digit. The tool renames those fields and, in json_serializable mode, adds @JsonKey(name: 'class') so the original key still maps. In plain mode the original key is used directly in fromJson.

What type do null values get?

A key that is null everywhere in the sample, like "rating": null on an unrated trip, becomes final dynamic rating; and is read with a plain json['rating']. When one record has a value and another has null or no key at all, the value's type is kept and, with Nullable Types on, gets a ?: the stop note in the trip above turns into String?.

How are large integers handled?

They stay int up to 64 bits, which is exact on the Dart VM. Values beyond that become num. If your app is compiled to the web, remember that numbers past 2^53 lose precision there; keep big IDs as strings in the API if you can.

What if my JSON root is an array?

Then the objects inside it supply the class, named after Class Name, and the first line of output is a comment saying to decode into List<Trip> if you typed Trip. A root such as ["a","b"] has no keys to model, so the panel explains that there is nothing to build a class from, and says what the root actually was.

Is my JSON sent anywhere?

No. The classes are generated by JavaScript running on this page. Nothing is uploaded, and input is capped at 256 KB.

Other JSON Tools You Might Need

Same API, other end of the stack? These write matching types for TypeScript and Java, and the parser, validator and formatter help when the response itself looks off: