JSON to Dart Class Generator
CV-73Paste a JSON sample and press Convert to get classes with fromJson and toJson. It infers the types from every item in your sample, and it runs in your browser.
Runs in your browser. Nothing is uploaded.
JSON
Dart
How to use it
- Paste a JSON sample into the input box, or open a .json file. Use a sample that shows every key you expect.
- Choose fromJson and toJson written out, or json_serializable annotations, and keep or drop the final fields.
- Press Convert, then copy the result or download it as a .dart file.
- Decode with jsonDecode and call Root.fromJson(map), then call toJson() to write it back.
How the classes are built
Each object becomes a class with fields in camelCase, a constructor with named parameters, a fromJson factory and a toJson method. Types are int, double, String, bool and List<T>; a number with a decimal point becomes double, and fromJson reads it through num so a whole-number value still loads. A key that is null or missing in some items gets a nullable type (T?), and a key missing from some objects is also left out of toJson while it is null. Mixed types, empty arrays and keys that are always null become dynamic, and Dart words such as class or default get a trailing underscore.
How the JSON is read
Every object in the sample is merged by key, so a key missing from some objects is marked optional and a null in some items marks the key nullable. If the document is a top-level array, its items are merged into one type named after the root name with Item added. The JSON is read with a parser that keeps number text exactly, so a big integer is not rounded before its type is chosen. Keys that are not valid Dart names, such as first-name, are renamed, and the original key is kept in the fromJson and toJson code (or the @JsonKey annotation).
What a sample cannot tell it
The result only reflects the sample you paste: a key that never appears has no member, and a number that is always whole in the sample is typed as whole even if other data holds decimals. Dates stay String; parse them with DateTime.parse in the fromJson code if you need DateTime values. Check the result against the API documentation before you rely on it.
Questions people ask
How do I convert JSON to a Dart class?
Paste the JSON into the input box, choose your options and press Convert to get the classes. Decode with jsonDecode and call Root.fromJson(map), then call toJson() to write it back. The conversion happens in your browser.
Should I use fromJson and toJson or json_serializable for Dart?
The default writes fromJson and toJson by hand, so the file works with no extra packages. Choose json_serializable if your project already uses build_runner; the output then has annotations and a part directive, and you run the build to create the .g.dart file.
How are numbers typed in the Dart classes?
Whole numbers become int, and numbers with a decimal point become double. fromJson reads doubles through num, so a JSON 20 still loads into a double field. An integer beyond int64 becomes double with a comment on the field.
Why is a Dart field typed dynamic?
Mixed types, an empty array and a key that is always null have no single type in the sample, so the field is dynamic. Replace it with the type you expect, or paste a sample where the key holds a value.
What happens when my JSON starts with an array?
The tool reads every item and merges them into one type, so a key present in only some items becomes optional. The output names that type after the root name with Item added, and a comment at the top says the document is a list. The Dart classes are written from the parsed sample.
How are very large numbers in the JSON sample handled?
Number text is kept exactly while types are picked, so a value like 12345678901 gets a 64-bit type where the language has one, and an integer beyond 64 bits is flagged rather than silently rounded. JSON Schema only records integer or number. The Dart classes are written from the parsed sample.
About this tool
The classes are inferred from the JSON you paste, so they only know what the sample shows. Every object becomes a class with fields in camelCase, a constructor with named parameters, a fromJson factory and a toJson method, working on Map<String, dynamic>. Types are int, double, String, bool and List<T>; a number written with a decimal point, or beyond int64, becomes double (read through num, so 20 still loads into a double field), and a comment marks integers beyond int64. A key that is null or missing in some items gets a nullable type (T?) and is left out of toJson when it is missing; mixed types, always-null keys and empty arrays become dynamic. Dart words such as class or default get a trailing underscore. The json_serializable choice writes the annotations and a part directive instead, and you still run build_runner to create the .g.dart file. It does not detect dates. Everything runs in your browser; nothing is uploaded.