JSON to Dart Converter — Null-Safe Dart Class Generator

JSON to Dart

Convert JSON to null-safe Dart classes with json_serializable & fromJson constructors.

Free online JSON to Dart class converter. Paste any JSON object or API response to instantly generate strongly typed, null-safe Dart data models for Flutter and Dart backend applications. Supports json_serializable annotations for build_runner, manual fromJson and toJson factory constructors, DateTime parsing, nested object models, typed arrays (List<T>), and custom field naming with @JsonKey. Automatically handles snake_case to camelCase conversion and nullable property inference. Runs 100% locally in your browser with zero server uploads for complete data privacy.

Keywords: json to dart, json to dart converter, json to dart class generator, flutter json to dart, dart model generator, dart null safety json, json_serializable dart, dart fromjson tojson, flutter data model generator, json to flutter model, dart json serialization, dart class from json online, freezed dart model generator, json to dart null safe, dart object mapping, build_runner json_serializable

Tags: json, dart, flutter, converter, class, model, generator, null-safety, json-serializable

Browse all 41 Developer Tools tools →

JSON to Dart is also known as: JSON to Flutter Model Generator, JSON to Dart Converter, Dart fromJson toJson Generator, Flutter JSON Model Builder, json_serializable Generator, Dart Class Generator.

How to JSON to Dart Online

  1. Paste raw JSON: Paste your API response payload, webhook body, or sample JSON data into the editor.

  2. Set root class name: Specify the PascalCase name for your primary data model (e.g. "UserProfile", "OrderResponse", "ProductCatalog").

  3. Configure code generation options: Toggle sound null safety, choose between json_serializable annotations or manual fromJson/toJson factory constructors, and configure optional field handling.

  4. Generate Dart classes: Click "Generate" or press ⌘↵ (Ctrl+Enter on Windows/Linux) to instantly generate fully typed Dart class definitions.

  5. Inspect nested class hierarchy: Review the automatically extracted child classes for nested objects and generic typed collections (List<T>).

  6. Export & integrate: Copy the generated Dart code to your clipboard via ⌘⇧C or integrate it directly into your Flutter / Dart project alongside build_runner.

JSON to Dart Features

  • Dart 3 Sound Null Safety: Generates non-nullable types by default and cleanly flags nullable fields (e.g. String?, int?, DateTime?) when nulls or optional keys are detected in the payload.

  • json_serializable Integration: Emits standard @JsonSerializable() annotations, part file directives (part "model.g.dart";), and _$ModelFromJson / _$ModelToJson glue hooks for build_runner.

  • Zero-Dependency Manual Factories: Alternatively outputs self-contained factory Model.fromJson(Map<String, dynamic> json) and Map<String, dynamic> toJson() methods without requiring external packages.

  • Automated Class Decomposition: Recursively parses nested JSON objects and arrays into clean, independent Dart class structures with proper cross-model references.

  • Smart Type Inference: Accurately maps JSON primitives to Dart String, int, double, num, bool, and generic List<T> collections.

  • Robust Numeric Handling: Employs safe (json["key"] as num).toDouble() casting in manual factories to eliminate runtime _CastError crashes between integer and float JSON values.

  • ISO-8601 DateTime Recognition: Automatically detects ISO timestamp strings and emits typed DateTime fields with parsing logic.

  • Key Sanitization & @JsonKey Mapping: Automatically translates snake_case, kebab-case, and special-character JSON keys into idiomatic camelCase Dart identifiers while preserving the wire key via @JsonKey(name: "...").

  • Immutable Const Constructors: Emits const constructors with required and optional named parameters for optimal memory efficiency and Flutter widget integration.

  • Dart Reserved Keyword Protection: Automatically escapes and renames conflicting JSON keys matching Dart reserved words (e.g. default, class, is, in, switch, break).

  • 100% Client-Side Privacy: All parsing and code synthesis run entirely in your local browser sandbox without transmitting data to any remote server.

  • Developer Shortcut Ergonomics: Boost your velocity with ⌘↵ generation, ⌘⇧C instant copying, and ⌘⇧K input clearing.

Supported Formats & Dialects

The JSON to Dart supports 6 syntax formats and dialects for accurate parsing and processing.

json_serializable & build_runner Architecture
Standard production workflow for Flutter and Dart applications. Generates classes decorated with `@JsonSerializable()` and `@JsonKey(name: "...")`, connecting with `part "model.g.dart";` and delegating serialization to build_runner-generated `_$ClassFromJson` and `_$ClassToJson` functions.
Pure Null-Safe Dart (Manual Factory Constructors)
Lightweight, zero-dependency Dart data models containing self-contained `factory Model.fromJson(Map<String, dynamic> json)` and `Map<String, dynamic> toJson()` implementations. Ideal for utilities, micro-packages, or CLI scripts where adding code generator build steps is undesirable.
Dart 3 Sound Null Safety & Type Guards
Full compliance with Dart 3.0+ sound null safety rules. Distinguishes between definitely present non-null attributes (`final String name;` with `required this.name`) and potentially nullable values (`final String? bio;` with `this.bio`), preventing runtime null-dereference errors.
Nested Object & Polymorphic List Hierarchies
Recursively extracts inline JSON objects into discrete PascalCase Dart classes. Generates strongly typed generic collections such as `List<VariantItem>` or `List<String>` and maps nested arrays of objects via `.map((e) => Item.fromJson(e as Map<String, dynamic>)).toList()`.
ISO-8601 DateTime & Timestamp Mapping
Detects RFC 3339 and ISO 8601 UTC/offset date strings in JSON values, automatically emitting `DateTime` fields and converting them using `DateTime.parse(json["created_at"] as String)` and `.toIso8601String()`.
Key Transformation & @JsonKey Annotation Mapping
Converts wire protocol formats (`snake_case`, `kebab-case`, or space-delimited JSON property names) into Dart standard `lowerCamelCase` variable names, injecting explicit `@JsonKey(name: "original_key")` annotations or dictionary lookups.
All Guides
All Standards

Frequently Asked Questions

How does Dart 3 sound null safety work when converting JSON to Dart models?
Dart 3 enforces sound null safety, meaning types are non-nullable by default unless explicitly annotated with a question mark (`?`). When converting JSON, the generator inspects field values: if a field contains `null` or is omitted across objects in an array, it is declared as a nullable type (e.g., `final String? nickname;`) and instantiated as an optional constructor parameter (`this.nickname`). Fields with non-null values are declared non-nullable (`final String id;`) and marked with the `required` keyword in the constructor (`required this.id`). This guarantees that no runtime null-pointer exceptions occur once data is parsed.
What is the difference between json_serializable code generation and manual factory constructors?
`json_serializable` is the recommended approach for large Flutter projects. It uses Dart's build system (`build_runner`) to automatically generate serialization boilerplate in a separate `.g.dart` file based on `@JsonSerializable()` annotations. This reduces handwritten code and prevents deserialization bugs when schemas change. Manual factory constructors (`Model.fromJson` and `model.toJson`), by contrast, write the parsing logic directly inside your class using native `Map<String, dynamic>` type casts. Manual factories require zero external dependencies and work immediately without running a build runner.
How should numeric values (int vs double) be handled to avoid runtime CastError?
In JSON, all numbers are formatted similarly, but Dart strictly separates 64-bit integer (`int`) and 64-bit float (`double`). If an API returns an integer (e.g., `10`) for a field expected to be a floating-point number (`double`), direct casting via `json["price"] as double` will throw a runtime `_CastError` in Dart. Our generator solves this by casting numeric fields to Dart's base numeric class (`num`) first: `(json["price"] as num).toDouble()` or `(json["price"] as num?)?.toDouble()`. This safely accepts both `10` and `10.5` without crashes.
How are ISO-8601 date strings detected and parsed into Dart DateTime objects?
When the generator encounters string values matching standard ISO-8601 or RFC 3339 formats (such as `2026-09-06T12:00:00Z`), it assigns the field a `DateTime` type. In manual mode, it generates `DateTime.parse(json["createdAt"] as String)` in the constructor and `createdAt.toIso8601String()` during serialization. In `json_serializable` mode, the code generator automatically utilizes built-in DateTime converters.
How does the converter handle snake_case API keys and Dart reserved keywords?
JSON APIs commonly use `snake_case` (e.g. `user_id`, `is_active`) or kebab-case keys, whereas Dart style conventions mandate `lowerCamelCase` for class fields (`userId`, `isActive`). The generator transforms all keys into idiomatic camelCase identifiers while adding `@JsonKey(name: "user_id")` or referencing the original string key in manual `json["user_id"]` lookups. If a JSON key matches a Dart reserved keyword (such as `default`, `class`, `is`, `in`, `switch`, or `continue`), the generator renames the identifier safely (e.g., `defaultValue` or `inValue`) to prevent compilation errors.
How are deeply nested JSON objects and arrays (List<T>) mapped into separate Dart classes?
When an input JSON property contains an object or an array of objects, the generator decomposes each unique structure into its own independent PascalCase Dart class. The parent class references the child class by name (e.g., `final Address address;` or `final List<VariantItem> variants;`). In manual `fromJson` factories, nested arrays are parsed using collection transforms like `(json["variants"] as List<dynamic>).map((e) => VariantItem.fromJson(e as Map<String, dynamic>)).toList()`.
How do I configure build_runner and json_annotation in my Flutter pubspec.yaml?
To use `json_serializable` in Flutter or Dart, add `json_annotation` under `dependencies` and both `build_runner` and `json_serializable` under `dev_dependencies` in your `pubspec.yaml`: ```yaml dependencies: flutter: sdk: flutter json_annotation: ^4.9.0 dev_dependencies: build_runner: ^2.4.9 json_serializable: ^6.8.0 ``` After saving your generated Dart model file (e.g., `lib/models/user.dart`), execute `dart run build_runner build --delete-conflicting-outputs` in your terminal to generate the `user.g.dart` companion file.
Why does Flutter avoid runtime reflection (dart:mirrors) for JSON serialization?
Flutter compiles Dart code Ahead-Of-Time (AOT) to native ARM machine code for iOS and Android. Dynamic runtime reflection (via `dart:mirrors`) prevents dead-code stripping (tree shaking), substantially increasing mobile binary size and degrading performance. Because `dart:mirrors` is disabled in Flutter runtimes, Flutter relies exclusively on static compile-time code generation (`build_runner` + `json_serializable` or `freezed`) or explicit handwritten factory constructors.

Developer Reference & Learning Hubs