JSON to Pydantic Generator
Last updated:
Jsonic's JSON to Pydantic generator converts a JSON object into Pydantic v2 BaseModel class definitions. It infers Python types — str, int, float, bool — distinguishes integers from floats, wraps null-able fields in Optional, generates List[T] for arrays, and creates separate nested BaseModel classes for nested objects. Field names are converted to snake_case. The generator outputs ready-to-use Python code including all necessary imports from pydantic and typing. All conversion happens in your browser with no data upload.
How to convert JSON to a Pydantic model
- Paste your JSON object into the left panel, or click Example to load a sample.
- Optionally change the root class name (default: Model).
- Click Generate.
- The right panel shows the Pydantic v2 BaseModel class definitions with correct Python types.
- Copy the code and paste it into your Python project.
FAQ
What Pydantic version does this generate for?
The generator outputs Pydantic v2 syntax using class Model(BaseModel): with standard field annotations. Pydantic v2 is the current version as of 2024 and uses the same field annotation style as v1, so the output is compatible with both.
How are types inferred from JSON?
The generator maps JSON types to Python types: JSON strings → str, JSON integers → int, JSON decimals → float, JSON booleans → bool, JSON null → Optional[T], JSON arrays → List[T], and JSON objects → nested BaseModel subclass. When an array contains objects, a separate nested model is generated for the item type.
How are nullable fields handled?
If a JSON field is null or if the same field appears as both a value and null across multiple array items, the generator wraps the type in Optional[T] and adds Optional to the typing imports.
What happens to camelCase field names?
camelCase keys are converted to snake_case to follow Python conventions. For example, "userId" becomes user_id and "firstName" becomes first_name. You can add a model_config with populate_by_name=True if you need to accept both forms.
Can it handle nested objects?
Yes. Each nested JSON object is converted into a separate BaseModel subclass. Nested models are declared before the root model so Python can resolve forward references without __future__ annotations.
Can it handle arrays of objects?
Yes. An array of objects generates a List[NestedModel] annotation and a corresponding BaseModel class for the item type. If the array contains objects with inconsistent keys across items, the generator merges them and marks missing keys as Optional.
What if I have an API response with a top-level array?
The tool works best with a top-level JSON object. For top-level arrays, paste one array item as the input and use List[Model] in your application code. This gives you a named model for the item type, which is more useful than a bare List annotation.
Is the generated code ready to use?
Yes. The output includes all necessary imports (from pydantic import BaseModel, from typing import ...) and class definitions in dependency order. Paste the code directly into your Python file and use Model(**data) or Model.model_validate(data) to parse your JSON.