formatjson.pro
Validate, pretty-print or minify JSON in your browser — nothing is uploaded.

JSON to Dart Class

Paste a JSON object and get idiomatic, null-safe Dart classes with a required-parameter constructor, a fromJson factory and a toJson method. Nested objects and arrays become their own classes — ready to drop into a Flutter model without build_runner or json_serializable.

Runs entirely in your browser — your JSON is never uploaded to a server. No sign-up, no ads in the output. Emits null-safe Dart classes with required constructors, fromJson factories and toJson methods, nesting classes recursively — ready for Flutter's jsonDecode/jsonEncode without build_runner.

How to generate a Dart class from JSON

  1. Paste your JSON. Paste one representative JSON object into the box. Use a sample that contains every field you expect, so types are inferred correctly.
  2. Generate the class. Click Generate Dart class. The tool infers Dart types for each field and creates nested classes for nested objects.
  3. Copy into your model file. Copy the output and paste it into a Dart file. Use Root.fromJson(jsonDecode(body)) to parse and jsonEncode(model.toJson()) to serialize.

How the type mapping works

Each JSON value becomes a strongly-typed, null-safe Dart field. A whole number maps to int, a decimal to double, text to String, true/false to bool, and null to dynamic. A JSON object becomes its own nested class; an array becomes a List<T> whose element type is inferred from the first item. Every class gets a const-friendly constructor with required named parameters, a fromJson factory, and a toJson method — the exact shape jsonDecode/jsonEncode expect in Flutter.

No code generation step needed

Packages like json_serializable require build_runner and generated .g.dart part files. The classes here are hand-written equivalents you can paste straight into a model file — useful for a quick prototype, a small app, or when you want to read exactly what the (de)serialization does. Field names are sanitized to valid Dart identifiers; the original JSON keys stay intact inside fromJson/toJson.

Using it with http

After final data = jsonDecode(response.body); call Root.fromJson(data). To send a model back, use jsonEncode(model.toJson()). Nested objects and lists are converted recursively, so a single fromJson call rebuilds the whole tree.

FAQ

Does this need build_runner or json_serializable?

No. The generated classes include hand-written fromJson and toJson, so there is no code-generation step and no .g.dart part files. Paste the output straight into your project.

Are the fields null-safe?

Yes. Fields are non-nullable and the constructor uses required named parameters, matching Dart's sound null-safety. JSON nulls map to dynamic; if a key may be missing, make that field nullable by hand (add a ? to the type).

How are nested objects and arrays handled?

A nested JSON object becomes its own class with its own fromJson/toJson. A JSON array becomes List<T>; if the elements are objects, each is rebuilt with T.fromJson, otherwise the list is cast to the primitive type.

What happens to keys that aren't valid Dart identifiers?

They are sanitized (non-alphanumeric characters become underscores, leading digits get a leading underscore) for the field name, while the original key string is kept inside the json['...'] lookups so (de)serialization stays correct.

Is my JSON uploaded anywhere?

No. The conversion runs entirely in your browser with JavaScript. Nothing is sent to a server, there is no sign-up, and no ads are injected into the output.