JSON to Dart model

Paste JSON and get Dart model classes for it below, as you type: final fields, a const constructor, a fromJson factory and a toJson method, as Flutter apps write them without a code generator.

A fromJson that reads everything

Nested objects are read by their own fromJson, lists with map(…).toList(), and objects keyed by ids or dates with MapEntry. A double reads whole numbers too ((json['price'] as num).toDouble()), as jsonDecode gives 10 for 10.0.

Null safety

A value that is null somewhere, or a key some items lack, is nullable (String?) and not required by the constructor; everything else is required.

Types from sample data

Every item of a list, and every value of JSON Lines, is a sample of the same type: a key that some lack is optional, a value that is sometimes null is nullable, whole numbers mixed with decimals are decimals, and objects with the same keys share one type, named after the key they are under ("line_items" holds LineItem). An object whose keys are ids or dates, such as {"1001": {…}, "1002": {…}}, is a map. YAML, TOML, CSV and the other formats work too: pick their language above the editor.

Or the data as a Map literal

Data writes the JSON as a <String, dynamic>{…} literal with single-quoted strings, for tests and mock data.

Copy, download or switch

The Dart follows your typing, and a mistake in the JSON is pointed out first, with its line and column. Copy or Download (a .dart file) take it as it is. Above the code, switch between Types and Data, or pick another language; the Convert menu's Code item opens the one you picked last.

Private: nothing is uploaded

Formatting, checking and comparing all happen in your browser. Your documents are saved in the browser's own storage on this computer, not on a server, so they never leave your device.

With your permission, we count anonymous visits (which pages and tools are used), never what you type. See Privacy.

Questions

Do I need json_serializable or build_runner?

No: fromJson and toJson are written out, so the classes work as they are. To move to json_serializable later, keep the fields and let it generate the two methods.

How do I read the JSON?

Root.fromJson(jsonDecode(text) as Map<String, dynamic>), with dart:convert imported.

Is my data uploaded anywhere?

Your documents aren't: everything runs in your browser, and they are saved on your computer, in the browser's storage for this site. With your permission we count anonymous visits, never their content.

More tools