ToolLineup

JSON to Dart

Paste a JSON payload and get Dart classes with final fields, a named constructor, a fromJson factory and a toJson method — the complete round trip, with no code generator or build_runner step. Double fields are read through num.toDouble() rather than cast directly, which avoids the most common crash in hand-written Dart JSON code: a whole-numbered JSON value decodes as int, and casting an int to double throws.

How it works

Paste a representative JSON sample and the Dart declarations appear on the right. The types are inferred from what is actually in the sample, so the more complete your example, the better the result — one array element with an extra field is enough to have that field marked optional rather than missed.

Each object becomes a class with final fields, a named constructor, a fromJson factory and a toJson method — the whole round trip, with no build step or code generator needed.

A double field is read through num.toDouble() rather than cast directly. This matters: a JSON number with no fractional part decodes as int, and casting an int to double throws at runtime — which is the single most common crash in hand-written Dart JSON code.

Generation runs entirely in your browser — nothing you paste is uploaded.

JSON sample
Paste a representative JSON payload…
Dart classes with fromJson and toJson

Frequently asked questions

Why is a double field read with num.toDouble() instead of cast directly?

A JSON number with no fractional part — 34 rather than 34.5 — decodes to an int in Dart, and casting an int to double throws a type error at runtime. Going through num.toDouble() handles both cases, which is why it is generated that way rather than as a plain cast.

Do I need build_runner or json_serializable?

No. The fromJson and toJson methods are written out in full, so the classes work as soon as you paste them in — there is no annotation to process and no generated part file to keep in sync.

How are optional fields handled?

A field missing from some records in the sample becomes a nullable type with a question mark, and its constructor parameter drops the required keyword — so an absent key decodes to null rather than throwing.

What happens to a JSON key that is not a valid Dart identifier?

Keys are converted to camelCase for the Dart field name while fromJson and toJson keep reading and writing the original key, so a key like ship-to becomes shipTo without breaking the wire format.

Does this work for nested objects and lists?

Yes — a nested object gets its own class and is decoded through its fromJson factory, and a list of objects is mapped element by element rather than cast wholesale, which is what makes the nested types actually materialise.

Related tools