JSON to Pydantic v2 Generator
Convert JSON payloads into Python Pydantic v2 BaseModel classes with snake_case field aliases, ConfigDict(populate_by_name=True), Optional types, and datetime detection.
How to Use
- Paste a JSON object or array into the input editor or click Sample to load a multi-model example.
- Customize the root model name and toggle Optional[...], datetime detection, or snake_case aliases.
- Copy the Python code or click Download .py to save models.py directly to your machine.
What is JSON to Pydantic v2 Generator?
A JSON to Pydantic v2 Generator converts JSON objects and arrays into typed Python BaseModel classes tailored for Pydantic v2, FastAPI, and modern Python data pipelines. It automatically produces PEP-8 snake_case attributes, Field(alias=...) mappings, ConfigDict settings, and bottom-up class definitions.
How It Works
After parsing JSON locally via JSON.parse(), the engine recursively traverses objects and arrays, merging object array items to identify Optional[...] fields and int/float promotions. It detects ISO-8601 date and datetime strings, converts JSON keys to snake_case while preserving original wire names via Field(alias=...), and emits minimal, clean Python imports.
Common Use Cases
- Building FastAPI request and response schemas — convert sample API payloads into validated BaseModel classes
- Bridging camelCase JSON APIs to snake_case Python — preserve wire compatibility with Field(alias=...) and populate_by_name
- Typing LLM tool outputs and JSON feeds — inspect nested lists and nullable keys across heterogeneous records
- Keeping sensitive data local — scaffold Python models from proprietary payloads strictly inside your browser
Technical Details
Pure client-side Pydantic v2 code generator (generatePydanticModels) in src/tools/json-to-pydantic.tsx. Emits bottom-up topological BaseModel ordering, PEP-8 snake_case normalization with Python keyword/underscore guards, ConfigDict(populate_by_name=True), Optional[...], list[...], Union[...], and ISO date/datetime detection.
FAQ
Does it emit Pydantic v2 syntax instead of legacy v1 Config classes?
Yes. Whenever camelCase or reserved JSON keys require aliases, the generator emits Pydantic v2 model_config = ConfigDict(populate_by_name=True) alongside Field(alias="...") and modern list[...] annotations.
How are camelCase keys and Python reserved keywords handled?
Keys such as firstName or apiURL are converted to PEP-8 snake_case (first_name, api_url) and paired with Field(alias=...). Python keywords like class are escaped as class_ with an alias back to "class".
In what order are nested BaseModel classes generated?
Nested child classes are emitted before parent classes in bottom-up topological order so you can paste or run the generated Python module immediately without forward-reference errors.