JSON to Dart Model Generator for Flutter

Paste JSON and get Dart classes with fromJson and toJson, nested models, List<T> and null safety, written by hand or for json_serializable. Runs in your browser, so your data is never uploaded.

JSON Input

Ln 1, Col 1Empty

Dart

Your result will appear here.
No result yet

What Is a JSON to Dart Converter?

Flutter apps get their data as JSON, but Dart code works best with typed classes. A JSON to Dart converter reads an example response and writes those model classes for you: one class per object, the right types, null safety for values that can be missing, and the fromJson and toJson methods that turn JSON into objects and back.

How to Convert JSON to Dart Classes

  1. Paste a sample of your JSON, or drop a .json file on the box.
  2. Choose a class name and a style: plain fromJson/toJson, or json_serializable.
  3. Click Generate Dart, or press Ctrl + Enter.
  4. Copy the code, or download it as a .dart file.

Example

JSON
{
  "id": 312,
  "title": "Garlic Butter Pasta",
  "rating": 4.7,
  "is_vegan": false,
  "photo_url": null,
  "tags": ["quick", "dinner"],
  "author": {
    "name": "Olivia Brown",
    "verified": true
  },
  "ingredients": [
    { "name": "Spaghetti", "grams": 200 },
    { "name": "Garlic", "grams": 15, "optional": false }
  ]
}
Dart (fromJson / toJson)
class Recipe {
  final int id;
  final String title;
  final double rating;
  final bool isVegan;
  final dynamic photoUrl;
  final List<String> tags;
  final Author author;
  final List<Ingredient> ingredients;

  Recipe({
    required this.id,
    required this.title,
    required this.rating,
    required this.isVegan,
    this.photoUrl,
    required this.tags,
    required this.author,
    required this.ingredients,
  });

  factory Recipe.fromJson(Map<String, dynamic> json) => Recipe(
        id: json['id'] as int,
        title: json['title'] as String,
        rating: (json['rating'] as num).toDouble(),
        isVegan: json['is_vegan'] as bool,
        photoUrl: json['photo_url'],
        tags: (json['tags'] as List<dynamic>).map((e) => e as String).toList(),
        author: Author.fromJson(json['author'] as Map<String, dynamic>),
        ingredients: (json['ingredients'] as List<dynamic>).map((e) => Ingredient.fromJson(e as Map<String, dynamic>)).toList(),
      );

  Map<String, dynamic> toJson() => {
        'id': id,
        'title': title,
        'rating': rating,
        'is_vegan': isVegan,
        'photo_url': photoUrl,
        'tags': tags,
        'author': author.toJson(),
        'ingredients': ingredients.map((e) => e.toJson()).toList(),
      };
}

class Author {
  final String name;
  final bool verified;

  Author({
    required this.name,
    required this.verified,
  });

  factory Author.fromJson(Map<String, dynamic> json) => Author(
        name: json['name'] as String,
        verified: json['verified'] as bool,
      );

  Map<String, dynamic> toJson() => {
        'name': name,
        'verified': verified,
      };
}

class Ingredient {
  final String name;
  final int grams;
  final bool? optional;

  Ingredient({
    required this.name,
    required this.grams,
    this.optional,
  });

  factory Ingredient.fromJson(Map<String, dynamic> json) => Ingredient(
        name: json['name'] as String,
        grams: json['grams'] as int,
        optional: json['optional'] as bool?,
      );

  Map<String, dynamic> toJson() => {
        'name': name,
        'grams': grams,
        'optional': optional,
      };
}

is_vegan becomes the Dart-style name isVegan while fromJson still reads the original key. optional is bool? and not required, because only one ingredient has it. photo_url is always null in the sample, so its type is unknown and stays dynamic: change it to String? if it holds a URL.

How the Types Are Chosen

JSON valueDart type
TextString
Whole numberint
Number with decimalsdouble, read with (value as num).toDouble()
true / falsebool
ObjectIts own class, like Author
ArrayList<T>, like List<Ingredient>
Missing in some items, or nullA nullable type, like int?, and an optional constructor parameter
Only null, or mixed typesdynamic. Replace it with the real type if you know it.

Why (value as num).toDouble()? Dart decodes a JSON number without a decimal point, like 5, as an int. A plain as double would throw for that value, so the code accepts any number and converts it.

Use the Model in Flutter

Decode a response with the http package
import 'dart:convert';
import 'package:http/http.dart' as http;

Future<Recipe> fetchRecipe() async {
  final response = await http.get(Uri.parse('https://api.example.com/recipes/312'));
  if (response.statusCode != 200) {
    throw Exception('Request failed: ${response.statusCode}');
  }
  return Recipe.fromJson(jsonDecode(response.body) as Map<String, dynamic>);
}

To send it back, call jsonEncode(recipe): jsonEncode calls your toJson() method automatically. If the JSON is an array at the top level, map each item: (jsonDecode(body) as List<dynamic>).map((e) => Recipe.fromJson(e as Map<String, dynamic>)).toList().

Plain fromJson or json_serializable?

  • Plain fromJson / toJson needs no packages and no build step. Every line is visible, which makes it easy to adjust a field by hand. Good for small apps and a handful of models.
  • json_serializable writes the conversion code for you in a .g.dart file. Add json_annotation to your dependencies and json_serializable plus build_runner to your dev dependencies, then run dart run build_runner build. The part 'recipe.g.dart'; line must match your file name, so save the code as recipe.dart.

Tips

  • Numbers that are sometimes decimals: if a field is 5 in your sample but can be 5.5 in real data, as int will throw. Change the type to double or num.
  • Dates: date strings stay String. Parse ISO 8601 values like 2026-11-18T09:30:00Z with DateTime.parse().
  • Use a complete sample. The generator only sees the fields in your example, so include an item that has every optional field.
  • Check the JSON first. jsonDecode throws a FormatException on broken JSON. Fix it with JSON Repair or find the error in the JSON Validator.

Your JSON never leaves your browser, so it is safe to paste real API responses. Building the backend too? Try JSON to Kotlin, JSON to TypeScript or JSON to Go.

Frequently Asked Questions

How do I convert JSON to a Dart class?

Paste a sample of your JSON into the converter and click Generate Dart. It writes null-safe Dart classes with fromJson and toJson, nested models and List<T>, ready for a Flutter app.

Should I write fromJson by hand or use json_serializable?

Hand-written fromJson and toJson need no packages or build step, which suits small apps. json_serializable generates the code with build_runner, which saves work when you have many models. The converter can write either style.

Why is a double read with (value as num).toDouble()?

Dart decodes a JSON number without a decimal point, like 5, as an int, so casting it with as double would throw. Reading it as num and calling toDouble() works for both 5 and 5.5.

Is my JSON uploaded?

No. The Dart code is generated in your browser and your JSON never leaves your device.

Related Tools