toolqik

143 · JSON

JSON to Dart

Generate null-safe Dart classes with fromJson and toJson for Flutter.

Runs in your browser
Style
Options
Dart code

  

About this tool

Paste a JSON response and get Dart model classes for Flutter or Dart on the server. Classes are null-safe: required fields use required this.x, and values that were null or missing become nullable (String?). Keys like full_name become fullName.

Two styles

  • fromJson / toJson writes the conversion code by hand: no packages and no build step. Nested objects call their own fromJson, lists map each item, and numbers are read through num so 1 and 1.0 both work.
  • json_serializable adds @JsonSerializable() and @JsonKey(name: …), and the code is generated by dart run build_runner build. Add json_annotation to dependencies and json_serializable and build_runner to dev dependencies.

Using the classes

import 'dart:convert';

final root = Root.fromJson(jsonDecode(body) as Map<String, dynamic>);
final body2 = jsonEncode(root.toJson());

How types are inferred

  • Every object becomes its own named type. Names come from the key (address → Address), and items of an array get the singular (orders → Order).
  • For arrays of objects, all items are merged into one type: a field missing from some items becomes optional, and a field that is sometimes null becomes nullable.
  • Integers and decimals in the same field merge to a decimal type. Fields with mixed types (say a number in one item and a string in another) fall back to the language’s “any” type.
  • Identical nested shapes are reused instead of generating duplicates.

The code is generated from examples, so it can only be as accurate as the sample. Paste a response that includes optional fields and nulls (or several items) for the best result, then check the types against the API documentation. Generated in your browser: your JSON is not uploaded.

FAQ

How do I convert JSON to a Dart class?

Paste the JSON above. A class with a fromJson factory and a toJson method is generated for each object.

Should I use json_serializable?

For large models it saves maintenance: the code is regenerated when you change a class. For a few models, the hand-written style needs no build step.

Why is a number read as num?

JSON doesn’t distinguish 1 from 1.0, and Dart’s jsonDecode may return either int or double, so the code casts to num and converts.

Is the code null-safe?

Yes. It targets Dart 3 with sound null safety.

esc
↑↓ navigate↵ open