DEV Community

V . Raagull Sakthivel
V . Raagull Sakthivel

Posted on

🧬 Generate Dart Models from JSON — Meet json_model_gen

I just released json_model_gen, a CLI tool that turns raw JSON into clean Dart model classes — right from your terminal.

✨ Why use it?

🔄 Convert raw JSON into Dart models
❄️ Supports freezed
♻️ Supports equatable
🔒 Optional field nullability
💬 Interactive mode if you skip flags
🛑 Prevents accidental file overwrites
📁 Auto-renames .json → .dart when needed

🚀 Quick Start

dart pub add json_model_gen
Enter fullscreen mode Exit fullscreen mode

Run it:

dart run json_model_gen
Enter fullscreen mode Exit fullscreen mode

Or use all flags:

dart run json_model_gen \
  --input=raw.json \
  --output=lib/user_model.dart \
  --class=UserModel \
  --freezed \
  --equatable \
  --nullable
Enter fullscreen mode Exit fullscreen mode

📌 Example Output

Given:

{ "id": 1, "name": "Alice", "active": true }
Enter fullscreen mode Exit fullscreen mode

You get:

@freezed
class UserModel with _$UserModel {
  const factory UserModel({ int? id, String? name, bool? active }) = _UserModel;
  factory UserModel.fromJson(Map<String, dynamic> json) => _$UserModelFromJson(json);
}
Enter fullscreen mode Exit fullscreen mode

🧠 Why I built this``

I wanted a tool that fits right into your local dev workflow — no switching tabs, just raw JSON → model class in seconds. Also useful to keep the JSON alongside models for better documentation.

Try it out from pub.dev and let me know what you think! 💬
[(https://pub.dev/packages/json_model_gen)]
Feedback, ideas, and PRs are always welcome 🚀

Top comments (0)