Skip to content

📊 Generate from JSON Data

Generate Pydantic models directly from JSON data. Under the hood, the generator uses GenSON to infer a JSON Schema from your input, then processes it the same way as JSON Schema input.

🚀 Quick Start

datamodel-codegen --input pets.json --input-file-type json --output model.py

📝 Example

pets.json

{
  "pets": [
    {
      "name": "dog",
      "age": 2
    },
    {
      "name": "cat",
      "age": 1
    },
    {
      "name": "snake",
      "age": 3,
      "nickname": "python"
    }
  ],
  "status": 200
}

✨ Generated model.py

# generated by datamodel-codegen:
#   filename:  pets.json
#   timestamp: 2020-04-27T16:08:21+00:00

from __future__ import annotations

from pydantic import BaseModel


class Pet(BaseModel):
    name: str
    age: int
    nickname: str | None = None


class Model(BaseModel):
    pets: list[Pet]
    status: int


📖 See Also