JSON to Dart Converter

Step 1 / 333%

Generate dependency-free Dart model classes from a JSON object or an array of object samples. The converter merges every sampled array item, creates nested classes, preserves original JSON keys and produces sound null-safe field types with a factory fromJson constructor. Add toJson methods when you need output compatible with jsonEncode. Your sample and generated code stay in this browser and are never uploaded or placed in the URL.

How it works

  1. 1

    Paste a representative sample

    Use one JSON object or an array of objects. Multiple items help reveal missing, nullable and mixed-type fields.

  2. 2

    Choose Dart options

    Name the root class, decide whether every field should be nullable and include toJson methods if needed.

  3. 3

    Review and export

    Inspect the inferred classes, then copy the code or download a local models.dart file.

How JSON becomes null-safe Dart models

Dart’s official dart:convert documentation explains that jsonDecode() produces JSON-compatible values such as numbers, strings, Booleans, nulls, lists and string-keyed maps. A model class is not directly JSON-encodable, but jsonEncode() can call its toJson() method. This converter generates that manual serialization pattern without adding json_serializable annotations or package dependencies.

Suppose a sample contains a user with a nested address. The generated User has final fields and a named-parameter constructor. User.fromJson(Map<String, Object?> json) casts scalar values and calls UserAddress.fromJson() for the nested object. If address is absent or null in any sampled object, its type becomes UserAddress? and the factory checks for null before constructing it. When toJson is enabled, nested models are converted recursively into maps that jsonEncode() accepts.

JSON observation Generated Dart type
Whole number int
Fractional number double
Whole and fractional samples num
Text String
Boolean bool
Nested object A generated model class
Consistent array List<T>
Empty or incompatible nested array List<Object?>
Null-only or conflicting values Object?

The converter examines every object in a root array rather than trusting only the first item. A field becomes nullable when it is missing from one item or explicitly null. You can also make every field nullable when the real API is less predictable than the sample. The Dart null-safety guide notes that types are non-nullable by default and need ? to accept null.

Identifier handling and inference limits

JSON keys do not have to be valid Dart members. Punctuation is removed, words are converted to lowerCamelCase, reserved keywords receive a safe suffix and collisions receive deterministic numeric suffixes. The original key remains unchanged in json['original-key'] and in the toJson() map. Nested class names use their full path so unrelated address objects do not silently become one class.

One sample cannot prove an API contract. JSON strings are not automatically dates, UUIDs or enums, and an empty array provides no element type. Duplicate object member names are also not round-trip safe: JSON parsers commonly retain the last value. Review the generated models against the API documentation or schema before using them in production. The tool limits input, nesting, inferred declarations and output size to keep browser work bounded.

Frequently Asked Questions

No. Parsing, inference, code generation, copying and downloading happen in your browser. The JSON and generated Dart are not sent to our servers or placed in the URL.

Yes. Flutter uses Dart, so these dependency-free classes can be used in a Flutter project. Review the inferred types against the real API contract first.

No. It generates manual fromJson and optional toJson methods with no package or build-runner dependency.

A field observed as null or absent becomes nullable. You can choose to make every generated field nullable when the sample is not representative.

No. The result reflects only observed values. Use multiple object samples and compare the result with the provider’s schema or documentation.

Related Tools

Tool available in other languages