JSON to Code

JSON to Dart

Paste a JSON sample. Get Dart types, and the code that reads and writes the JSON. It runs in your browser, and your data never leaves it.

Open the tool with Dart chosen

An example

This JSON:

{
  "id": 42,
  "name": "Ada Lovelace",
  "active": true,
  "joined": "2020-01-02T03:04:05Z",
  "tags": ["engineer", "writer"],
  "address": { "city": "London", "postcode": null },
  "scores": [9.5, 8, 10]
}

becomes this Dart:

class Root {
    final int id;
    final String name;
    final bool active;
    final DateTime joined;
    final List<String> tags;
    final Address address;
    final List<double> scores;

    Root({
        required this.id,
        required this.name,
        required this.active,
        required this.joined,
        required this.tags,
        required this.address,
        required this.scores,
    });

}

class Address {
    final String city;
    final dynamic postcode;

    Address({
        required this.city,
        required this.postcode,
    });

}

With the reader and the writer

The tool also writes the code that turns the text into those types, and back again. That code checks the shape as it reads, which is the part a type alone cannot do.

// To parse this JSON data, do
//
//     final root = rootFromJson(jsonString);

import 'dart:convert';

Root rootFromJson(String str) => Root.fromJson(json.decode(str));

String rootToJson(Root data) => json.encode(data.toJson());

class Root {
    final int id;
    final String name;
    final bool active;
    final DateTime joined;
    final List<String> tags;
    final Address address;
    final List<double> scores;

    Root({
        required this.id,
        required this.name,
        required this.active,
        required this.joined,
        required this.tags,
        required this.address,
        required this.scores,
    });

    factory Root.fromJson(Map<String, dynamic> json) => Root(
        id: json["id"],
        name: json["name"],
        active: json["active"],
        joined: DateTime.parse(json["joined"]),
        tags: List<String>.from(json["tags"].map((x) => x)),
        address: Address.fromJson(json["address"]),
        scores: List<double>.from(json["scores"].map((x) => x?.toDouble())),
    );

    Map<String, dynamic> toJson() => {
        "id": id,
        "name": name,
        "active": active,
        "joined": joined.toIso8601String(),
        "tags": List<dynamic>.from(tags.map((x) => x)),

and 25 more lines. Open the tool to see the whole file.

What you need in your project

Nothing. `dart:convert` is in the standard library.

The file holds `fromJson` and `toJson` for each class, which is what Flutter projects use.

The code is plain Dart, so it needs no build step and no generated part file.

The trap to know about

A JSON number may arrive as `int` or as `double`. The reader handles both, but your own arithmetic must expect a `num`.

The options you can change

These belong to Dart, so the row of switches changes with the language.

OptionWhat it doesDefault
Types onlyTypes onlyfalse
Put encoder & decoder in ClassPut encoder & decoder in Classfalse
Required propertiesMake all properties requiredfalse
Make all properties finalMake all properties finaltrue
copyWithGenerate CopyWith methodfalse

Questions

How do I turn JSON into Dart?

Paste your JSON into the box, choose Dart, and the code appears at once. Give the top type a name, and every other name follows from your keys. Then copy the code, or download it as a file.

Does my JSON go to a server?

No. The generator runs inside your browser, in a worker. Nothing is uploaded, nothing is stored, and the tool works with the network turned off.

What do I need to add to my Dart project?

Nothing. `dart:convert` is in the standard library.

How does it know which fields are optional?

From your sample. A key that is missing, or holds null, becomes optional. One sample cannot show everything, so give more than one, or start from a JSON Schema.

Other languages

JSON to Swift JSON to Kotlin JSON to Objective-C All the guides

Try it on your own JSON. The generator runs in your browser. Nothing is uploaded, and nothing is stored on a server.

Open JSON to Code