JSON to Dart

Build null-safe Dart classes from a JSON sample with fromJson and toJson, nested models and typed lists. Parsing is browser-only.

Runs in your browserDeveloper ToolsFree · no signup

Dart · Dart 3 / null safety. Parsing and type inference happen in this browser tab. Nothing is uploaded and no network request is made.

Paste one object or an array of objects. Ctrl/⌘ + Enter also generates.
Used for the generated top-level class, struct or data class.
Starting point
The example has nested objects, a merged array and a null field.
0 characters · up to 5 MB is accepted; larger input is refused.

Is this tool not working?

Tell us what went wrong and we’ll fix it. The report is sent with the page you’re on (/json-to-dart/), so you don’t need to name the tool.

About the JSON to Dart

JSON to Dart builds null-safe model classes from a pasted JSON sample, with a fromJson factory and a toJson method on every class. It is aimed at Flutter and Dart API clients that want plain models without a build-time code generator. The whole job runs in the browser: the sample is parsed once, a type is inferred at each position, and each object becomes a class with camelCase fields, a constructor, a factory that reads the original keys from Map<String, dynamic>, and a method that writes those keys back. Nested objects become separate classes and call their own fromJson and toJson methods; lists become List<T> with per-element casts, including lists of lists; and values that were null or missing from one of several merged objects are emitted as nullable fields with non-required constructor parameters. All numbers are emitted as double and decoded through num with toDouble(), because one sample value cannot tell you whether a field is integral, how large it can grow or whether it needs fixed precision. Strings stay String and booleans stay bool. A null-only value, or a field whose array mixes incompatible kinds, becomes dynamic: Dart’s general type, which accepts any decoded JSON value and keeps the class usable until you decide on a concrete type. A top-level array of objects generates the element class and a comment recording the array root. The factories expect a decoded Map<String, dynamic>, so call jsonDecode first and pass the result; they do not accept a raw JSON string. Inference is sample-based and honest about its limits. A key present in your one object is assumed to be required; an empty array gives no information about its elements; and the generated code does not validate the payloads your app receives later. There is no inference of DateTime from strings, enum values from a small set, union types, custom converters or field-level validation. Reserved Dart keywords get an underscore suffix, duplicate names from different key spellings get numbered suffixes, and the original key is always what the factory reads and writes. Empty nested objects still get a working factory and toJson method. Nothing is uploaded: parsing and emission happen in your tab, with no account, no service call and no limit beyond your browser’s memory. Use the classes as a quick scaffold for a Flutter model layer, then replace dynamic with real types, add date parsing and decide which fields are genuinely required once you know the API contract. Generated models save boilerplate, but they are not a substitute for documented schemas or tests against real responses.

How to use it

  1. Paste a JSON object or an array of objects into the input box, or press Load example to insert a labelled sample.
  2. Choose the top-level class name in Root type name; Root is the default and nested class names come from your keys.
  3. Press Generate Dart, or use Ctrl/⌘ + Enter in the input, to infer the models locally in the browser.
  4. Copy the code or download the .dart file. Each class includes its constructor, fromJson factory and toJson method.
  5. Refine dynamic fields, dates and required flags using the real API contract before using the models in your app.

Worked example

Worked example: {"id":5,"user_name":"Grace","scores":[1,2],"address":{"city":"Paris"}} as Root yields class Root with final double id, final String userName, final List<double> scores and final Address address; fromJson reads json['user_name'], maps json['scores'] through (e) => (e as num).toDouble(), and calls Address.fromJson for address. toJson writes the same keys back.

Good to know

This tool processes everything locally with JavaScript. Nothing you paste or upload is transmitted to Hellagape or to any third party, and closing the tab clears the data. There is no daily limit and no account requirement. Results are provided for information only; for legal, financial or medical decisions, confirm the outcome with a qualified professional.

FAQ

JSON to Dart questions

Do the generated classes need a code generator package?

No. Each class has a hand-written factory and toJson method, so there is no build_runner, freezed or json_serializable dependency. You can paste the file into a Flutter project and call Root.fromJson on a decoded map immediately.

Why are numbers decoded as double?

One sample cannot prove that a field is always an int or that it fits in Dart’s integer range. The generated code uses (json['x'] as num).toDouble() so whole numbers and fractions both decode. Switch to int when the API contract says the value is always integral.

When does a field become dynamic?

When the sample gives no usable type: a value that is only null, or an array whose elements mix types that cannot share one Dart type. dynamic accepts whatever JSON produced, which keeps the model usable but removes static checking for that field.

Are nullable fields optional in the constructor?

Yes. A field inferred as nullable is declared as String? or List<T>? and left out of the required list. Non-null fields use required this.field. A key missing from one of several merged array elements is treated as nullable as well.

Does it infer DateTime, enums or validation?

No. Strings remain String, repeated string values are not turned into enums, and fromJson only casts values. It will not reject a missing required key or an out-of-range number. Add parsing and validation in your own code where the API requires it.

Is my JSON uploaded?

No. The sample is parsed by the browser and all four emitters are pure JavaScript in the page. There is no upload, no server-side storage and no network request involved in generating the Dart source.