convert_json_model Pub Version

Command line tool for generating Dart models (json_serializable) from Json file.

partly inspired by json_model.


on pubspec.yaml

  convert_json_model: #latest
  build_runner: ^1.12.2
  json_annotation: ^4.0.0
  json_serializable: ^4.0.3

install using pub get command or if you using dart vscode/android studio, you can use install option.

What?, Why?, How?


Command line tool to convert .json files into .dart model files and finally will generate .g.dart file(json_serializable)



You might have a system or back-end REST app, and you want to build a dart app. you may start create models for your data. but to convert from Dart Map need extra work, so you can use json_serializable, but it just to let you handle data conversion, you still need to type it model by model, what if you have huge system that require huge amount of models. to write it all up might distress you.


This command line tool let your convert your existing .json files(that you might have) into dart(json_serializable) files

Why not just use the existing command line library json_model instead

The json_model is great, cool structure, but it doesnt have recursive import which the feature that i want, and i want it automatically change variable to camelCase, i could write an issue and PR, but its hard to make a changes as it dont really have a clean code scalable structure, and have comments that i dont understand, and contributors last active on that repo is in June, i dont think i could wait any longer, so i made new one, some of core feature remain the same, but (may be) have a better structure.


it run through your json file and find possible type, variable name, import uri, decorator and class name, and will write it into the templates. Create/copy .json files into ./jsons/(default) on root of your project, and run pub run convert_json_model.


  "id": 2,
  "title": "Hello Guys!",
  "content": "$content",
  "tags": "$[]tag",
  "user_type": "@enum:admin,app_user,normal",
  "auth_state": "@enum:verified(2),authenticated(1),guest(0)",
  "user": "$../user/user",
  "published": true


pub run convert_json_model


flutter pub run convert_json_model


import 'package:json_annotation/json_annotation.dart';
import 'content.dart';
import 'tag.dart';
import '../user/user.dart';

part 'examples.g.dart';

class Examples {

  int id;
  String title;
  Content content;
  List<Tag> tags;
    get examplesUserTypeEnum =>[userType];
    set examplesUserTypeEnum(ExamplesUserTypeEnum value) => userType = _examplesUserTypeEnumValues.reverse[value];
  @JsonKey(name: 'user_type') String userType;
    get examplesAuthStateEnum =>[authState];
    set examplesAuthStateEnum(ExamplesAuthStateEnum value) => authState = _examplesAuthStateEnumValues.reverse[value];
  @JsonKey(name: 'auth_state') int authState;
  User user;
  bool published;

  factory Examples.fromJson(Map<String,dynamic> json) => _$ExamplesFromJson(json);
  Map<String, dynamic> toJson() => _$ExamplesToJson(this);

enum ExamplesUserTypeEnum { Admin, AppUser, Normal }
enum ExamplesAuthStateEnum { Verified, Authenticated, Guest }

final _examplesUserTypeEnumValues = _ExamplesUserTypeEnumConverter({
  'admin': ExamplesUserTypeEnum.Admin,
  'app_user': ExamplesUserTypeEnum.AppUser,
  'normal': ExamplesUserTypeEnum.Normal,

final _examplesAuthStateEnumValues = _ExamplesAuthStateEnumConverter({
  2: ExamplesAuthStateEnum.Verified,
  1: ExamplesAuthStateEnum.Authenticated,
  0: ExamplesAuthStateEnum.Guest,

class _ExamplesUserTypeEnumConverter<String, O> {
  Map<String, O> map;
  Map<O, String> reverseMap;


  Map<O, String> get reverse => reverseMap ??=, v) => MapEntry(v, k));

class _ExamplesAuthStateEnumConverter<int, O> {
  Map<int, O> map;
  Map<O, int> reverseMap;


  Map<O, int> get reverse => reverseMap ??=, v) => MapEntry(v, k));


Getting started

  1. Create a directory jsons(default) at root of your project
  2. Put all or Create json files inside jsons directory
  3. run pub run convert_json_model. or flutter packages pub run convert_json_model flutter project


this package will read .json file, and generate .dart file, asign the type of the value as variable type and key as the variable name.

DescriptionExpressionInput (Example)Output(declaration)Output(import)
declare type depends on the json value{...:any type}{"id": 1, "message":"hello world"},int id;
String message;
import model and asign type{...:"$value"}{"auth":"$user"}User auth;import 'user.dart'
import recursively{...:"$../pathto/value"}{"price":"$../product/price"}Price price;import '../product/price.dart'
asign list of type and import (can also be recursive){...:"$[]value"}{"addreses":"$[]address"}List<Address> addreses;import 'address.dart'
use json_annotation @JsonKey{"@JsonKey(...)":...}{"@JsonKey(ignore: true) dynamic": "val"}@JsonKey(ignore: true) dynamic val;-
import other library(input value can be array){"@import":...}{"@import":"package:otherlibrary/otherlibrary.dart"}-import 'package:otherlibrary/otherlibrary.dart'
Datetime type{...:"@datetime"}{"createdAt": "@datetime:2020-02-15T15:47:51.742Z"}DateTime createdAt;-
Enum type{...:"@enum:(folowed by enum separated by ',')"}{"@import":"@enum:admin,app_user,normal"}enum UserTypeEnum { Admin, AppUser, Normal }(include variable declaration)-
Enum type with values{...:"@enum:(folowed by enum separated by ',')"}{"@import":"@enum:admin(0),app_user(1),normal(2)"}enum UserTypeEnum { Admin, AppUser, Normal }(include variable declaration)-
write code independentally(experimental){"@_...":...}{"@_ // any code here":",its like an escape to write your own code"}// any code here,its like an escape to write your own code-


you can copy json below and generate using pub run convert_json_model command


Source File


  "id": 2,
  "username": "John Doe",
  "blocked": false



import 'package:json_annotation/json_annotation.dart';
part 'user.g.dart';
class User {

  int id;
  String username;
  bool blocked;

  factory User.fromJson(Map<String,dynamic> json) => _$UserFromJson(json);
  Map<String, dynamic> toJson() => _$UserToJson(this);

After that, json_serializable will automatically genereate .g.dart files


part of 'user.dart';
User _$UserFromJson(Map<String, dynamic> json) {
  return User() = json['id'] as int
    ..username = json['username'] as String
    ..blocked = json['blocked'] as bool;

Map<String, dynamic> _$UserToJson(User instance) => <String, dynamic>{
      'username': instance.username,
      'blocked': instance.blocked,

Asign Type variable

you can use $ to specify the value to be Type of variable

Source File


  "id": 2,
  "username": "John Doe",
  "blocked": false,
  "addresses": "$address" // prefix $

In this case, $address is like telling the generator to import address.dart and asign the titled case Address as it is the type of the variable addresses.



import 'package:json_annotation/json_annotation.dart';
import 'address.dart';  // automatic import
part 'user.g.dart';

class User {
  int id;
  String username;
  bool blocked;
  Address addresses;  // $address converted to Address as type
  factory User.fromJson(Map<String, dynamic> json) => _$UserFromJson(json);
  Map<String, dynamic> toJson() => _$UserToJson(this);

Asign List variable

you can use $[] to specify the value to be List of Type of variable

Source File


  "id": 2,
  "username": "John Doe",
  "blocked": false,
  "addresses": "$[]address" // prefix $[]



import 'package:json_annotation/json_annotation.dart';
import 'address.dart'; // write address as import
part 'user.g.dart';

class User {
  int id;
  String username;
  bool blocked;
  List<Address> addresses; // List of Type
  factory User.fromJson(Map<String, dynamic> json) => _$UserFromJson(json);
  Map<String, dynamic> toJson() => _$UserToJson(this);

json_serializable JsonKey

you can use @JsonKey in key to specify @JsonKey

Source File


  "@JsonKey(ignore: true) dynamic": "md", //jsonKey alias
  "@JsonKey(name: '+1') int": "loved", //jsonKey alias
  "name": "wendux",
  "age": 20



import 'package:json_annotation/json_annotation.dart';

part 'cart.g.dart';

class Cart {

  @JsonKey(ignore: true) dynamic md; // jsonKey generated
  @JsonKey(name: '+1') int loved; // jsonKey generated
  String name;
  int age;

  factory Cart.fromJson(Map<String,dynamic> json) => _$CartFromJson(json);
  Map<String, dynamic> toJson() => _$CartToJson(this);



  • imports import statement strings. Got from .json value with prefix $, suffixed it with .dart interpolate into import '$import';\n.
  • fileName file name. Got from .json value with prefix $, but the non-word caracter(\W) being removed, turn it intoCamelCase()
  • className class name. Basically fileName but turned intoTitleCase().
  • declarations declaration statement strings. basically list of DartDeclaration object and turned it intoString() .
  • enums any statements annotated as @enum will be parsed an added to the generated dart statements.
  • enumConverters to automatically bind the enum string value to the actual enum using a converter #


String defaultTemplate({
  }) =>  """
import 'package:json_annotation/json_annotation.dart';


part '$fileName.g.dart';

class $className {


  factory $className.fromJson(Map<String,dynamic> json) => _\$${className}FromJson(json);
  Map<String, dynamic> toJson() => _\$${className}ToJson(this);



for more info read model_template.dart


I'm open contribution for documentation, bug report, code maintenance, etc. properly submit an issue or send a pull request.


any typos, grammar error, unintended word, or ambiguous meaning. you can PR. or maybe create an issue. this is the one i really need your help


any bugs, unintended word comments, confusing variable naming. you can create an issue, but also a PR really appreciated.

Feature request

any missing feature, cool feature, like prefix json key command, or dynamic changing. you can create an issue, or write a dart extension for it.


if you want to help maintain this library, kindly read