Xerobit

JSON to Dart Class Converter

Use this json to dart class converter to turn any JSON object into typed Dart code complete with fromJson and toJson methods. Nested objects become their own classes, arrays are inferred from the first element, and null values default to dynamic. The json to dart class generation runs locally in your browser so your data is never uploaded.

Dart code

class Root {
  final String name;
  final int age;

  const Root({
    required this.name,
    required this.age,
  });

  factory Root.fromJson(Map<String, dynamic> json) {
    return Root(
      name: json['name'] as String,
      age: (json['age'] as num).toInt(),
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'name': name,
      'age': age,
    };
  }
}

How to use the json to dart class converter

  1. Paste your JSON object into the input area.
  2. Change the Root class name if you want a specific name for the top-level class.
  3. The Dart code updates automatically as you type or paste.
  4. Copy the generated classes into your Dart or Flutter project.

How the json to dart class conversion works

The converter parses your JSON into a JavaScript object and walks every key recursively. For each JSON object it finds, it emits a Dart class. Primitive types map directly: strings become String, integers become int, decimals become double, and booleans become bool. A null value maps to dynamic because the real type cannot be inferred without more data.

Nested objects trigger a new class definition. The field type uses the PascalCase version of the key name. Arrays inspect every element to determine the inner type; an array of objects is merged into one class and wrapped in List<ClassName>.

Type mapping reference

This table shows how each JSON value translates into a Dart type during the json to dart class process.

JSON valueDart typeExample
stringString"hello"
integerint42
decimaldouble3.14
booleanbooltrue
nulldynamicnull
objectClassName{"a":1}
array of primitivesList<Type>[1, 2]
array of objectsList<ClassName>[{"a":1}]

fromJson and toJson methods

Every generated class includes a factory ClassName.fromJson(Map<String, dynamic> json) constructor and a Map<String, dynamic> toJson() method. The factory reads each key from the map and casts it to the expected type. For nested objects it calls the child class's own fromJson. For lists it iterates over the JSON array and maps each element.

The toJson method does the reverse, building a map that can be passed directly to dart:convert's jsonEncode. A field is marked nullable (String?) when the key is null or missing in at least one object of the sample, and required otherwise. Values read from JSON numbers go through num, so both 1 and 1.0 decode safely into int and double fields.

Naming conventions and sanitization

Class names are derived by converting the JSON key to PascalCase. A key like user_profile becomes UserProfile. Field names use camelCase, so first-name becomes firstName. If a field name collides with a Dart reserved word such as class or return, the converter appends an underscore (class becomes class_) to keep the code compilable. Names never start with an underscore because Dart treats those as private and they cannot be used as named constructor parameters.

Fields keep the order of the keys in your JSON, and running the same JSON through the tool twice always produces identical Dart code, which helps when committing generated models to version control.

Working with other languages

If your project spans multiple platforms you may need model classes in several languages. This tool focuses on Dart and Flutter, but sibling converters handle other targets. You can convert the same JSON to a Java class, a C# class, a Go struct, a Python class, a Kotlin data class, a Rust struct, a Swift struct, or a PHP class. Each generator follows the idiomatic patterns of its target language.

Worked examples

ExampleInputResult
Simple userJSON input: {"name":"Alice","age":30,"active":true}; Root class name: Userclass User { / final String name; / final int age; / final bool active; / const User({ / required this.name, / required this.age, / required this.active, / }); / factory User.fromJson(Map<String, dynamic> json) { / return User( / name: json['name'] as String, / age: (json['age'] as num).toInt(), / active: json['active'] as bool, / ); / } / Map<String, dynamic> toJson() { / return { / 'name': name, / 'age': age, / 'active': active, / }; / } / }
Nested addressJSON input: {"name":"Bob","address":{"street":"123 Main St","city":"Springfield"}}; Root class name: Personclass Address { / final String street; / final String city; / const Address({ / required this.street, / required this.city, / }); / factory Address.fromJson(Map<String, dynamic> json) { / return Address( / street: json['street'] as String, / city: json['city'] as String, / ); / } / Map<String, dynamic> toJson() { / return { / 'street': street, / 'city': city, / }; / } / } / class Person { / final String name; / final Address address; / const Person({ / required this.name, / required this.address, / }); / factory Person.fromJson(Map<String, dynamic> json) { / return Person( / name: json['name'] as String, / address: Address.fromJson(json['address'] as Map<String, dynamic>), / ); / } / Map<String, dynamic> toJson() { / return { / 'name': name, / 'address': address.toJson(), / }; / } / }
Array of tagsJSON input: {"title":"Post","tags":["dart","flutter"]}; Root class name: Articleclass Article { / final String title; / final List<String> tags; / const Article({ / required this.title, / required this.tags, / }); / factory Article.fromJson(Map<String, dynamic> json) { / return Article( / title: json['title'] as String, / tags: (json['tags'] as List<dynamic>).map((e) => e as String).toList(), / ); / } / Map<String, dynamic> toJson() { / return { / 'title': title, / 'tags': tags, / }; / } / }

FAQ

Does this json to dart class tool upload my JSON to a server?

No. The conversion happens entirely in your browser using JavaScript. Nothing is sent over the network.

How are null values handled?

A null value in the JSON is typed as dynamic in Dart because the actual type cannot be determined. Provide a non-null sample value if you want a specific type.

Can I convert a JSON array at the root level?

If the array holds objects, the converter merges them into one class that describes a single element and marks keys missing from some objects as nullable. An array of plain numbers or strings is rejected because there is no record to turn into a class.

Are the generated fields nullable?

Only when the sample needs it. A field is nullable (with a question mark) if its value is null or its key is missing in at least one object; every other field is required in the constructor.

What happens if two nested objects share the same key name?

The converter deduplicates class names by appending a number. The first Address key becomes the Address class, and a second one becomes Address2.

Does it support Unicode characters in strings?

Yes. Unicode characters are preserved exactly as they appear in the JSON input, including emoji and non-Latin scripts.