JSON to Dart

Generate Dart types from JSON automatically.

Local Your input is processed locally in your browser. Found a problem? Report an issue

What JSON to Dart does

JSON to Dart turns a sample JSON document into Dart code: one class with a named constructor per object, with a type for every field inferred from the value in the sample.

JSON to Dart examples

The built-in sample

Input

{"name":"Ada Lovelace","active":true,"tags":["math","code"],"address":{"city":"London"}}

Output

class Address {
  final String city;
  Address({required this.city});
}

class Root {
  final String name;
  final bool active;
  final List<String> tags;
  final Address address;
  Root({required this.name, required this.active, required this.tags, required this.address});
}

This is the input that the Load sample button fills in.

Awkward keys, a null, a decimal and arrays

Input

{"order-id":7,"total":19.5,"paid":false,"coupon":null,"items":[{"sku":"A1","qty":2},{"sku":"B2","qty":1,"gift":true}],"tags":[]}

Output

class Items {
  final String sku;
  final int qty;
  final bool gift;
  Items({required this.sku, required this.qty, required this.gift});
}

class Root {
  final int order_id;
  final double total;
  final bool paid;
  final dynamic coupon;
  final List<Items> items;
  final List<dynamic> tags;
  Root({required this.order_id, required this.total, required this.paid, required this.coupon, required this.items, required this.tags});
}

The sample has a key with a hyphen (order-id), a null (coupon), a decimal (total), an array of objects whose second item has an extra field, and an empty array.

Each output was produced by running this tool on the input above it.

How it works

The sample is walked recursively and each value is mapped to a Dart type:

Sample valueDart type
StringString
Whole numberint
Decimal numberdouble
true / falsebool
null, or an empty array’s itemsdynamic
Array of TList<T>
Objecta new named type

Using the generated code

The classes have a constructor but no fromJson. jsonDecode returns maps and lists; write a factory constructor that reads them, or generate one with json_serializable:

final map = jsonDecode(text) as Map<String, dynamic>;

Details and limits

  • A name that starts with an underscore is private to its library in Dart, and most SDK versions reject a named parameter such as required this._id. Rename such fields.
  • A key that is a reserved word, such as class, is emitted unchanged and must be renamed.
  • Null values and empty arrays are typed dynamic. Replace it with a nullable type such as String?.
  • A type is named after the key that holds the object: items gives a type called Items, with no attempt to make it singular. The top-level type is always Root.
  • If two different objects sit under the same key name in different places, two types with the same name are generated. Rename one of them.
  • Objects in an array are merged into one type. A field that only some of them have is still generated as a required field.
  • Input that is not valid JSON stops the tool, and the status line shows where the problem is. The JSON Validator page lists the usual causes.

Questions

Is my data uploaded when I use JSON to Dart?

No. The tool runs in your browser, so what you paste or open is processed on your own device and is not sent to a server.

What does JSON to Dart do?

JSON to Dart turns a sample JSON document into Dart code: one class with a named constructor per object, with a type for every field inferred from the value in the sample.

Are there any limits I should know about?

A name that starts with an underscore is private to its library in Dart, and most SDK versions reject a named parameter such as required this._id. Rename such fields. A key that is a reserved word, such as class, is emitted unchanged and must be renamed.

Is there a size limit?

There is no fixed size limit. Everything happens on your device, so very large inputs depend on its memory and can take longer on a phone than on a computer.

Does it work on phones, and is it free?

Yes. It works in any current browser on Android phones, iPhones, tablets and computers. It is free and needs no account or sign-up.