JSON to Pydantic Model Generator
Generate Pydantic v2 model classes from a JSON sample instantly in your browser. Your JSON stays on your device.
Your JSON is parsed and converted into Pydantic models entirely in your browser, so it stays on your device and nothing is uploaded to a server.
Need Go, Rust or TypeScript types? Try JSON to Code.
About JSON to Pydantic
This json to pydantic generator turns a sample JSON object or array into ready-to-use Pydantic v2 model classes in seconds. Paste a real API response and it infers each field type, str, int, float and bool, marks nullable fields as Optional, unifies arrays into list[...] types, and promotes every nested object into its own BaseModel referenced by name. Keys that are not valid Python identifiers are kept through Field(alias=...) so the model still parses your original payload. It is built for Python and FastAPI developers who would rather generate pydantic models from json than hand-write them while wiring up a typed client. Everything runs locally in your browser, so the JSON you paste is processed on your device and nothing is uploaded to a server.
Features
- Generates Pydantic v2 BaseModel classes from any JSON object or array
- Infers str, int, float and bool from your sample values
- Marks nullable and sometimes-missing fields as Optional[...]
- Promotes nested objects into their own named BaseModel classes
- Unifies arrays into list[...] types, empty arrays become list[Any]
- Keeps invalid Python keys via Field(alias=...) so parsing still works
- Emits clean imports for BaseModel, Field, Optional and Any
- Copy the result or download it as a .py file
How to use the JSON to Pydantic
- Paste your JSON object or array into the input box.
- Set the Root model name to match your schema, or leave it as Model.
- Review the generated Pydantic classes in the output panel.
- Copy the result or download it as a .py file.
Example
Input
{ "id": 1, "name": "Ada", "address": { "city": "London" } }
Output
from pydantic import BaseModel
class Address(BaseModel):
city: str
class Model(BaseModel):
id: int
name: str
address: Address
A nested object becomes its own BaseModel, with the root class last.
Common errors & troubleshooting
- The output shows an Invalid error with a line and column. — The JSON has a syntax issue such as a trailing comma, single quotes, or an unquoted key. Fix it at the reported line and column, then the models regenerate.
- A field came out as Optional[Any] instead of a concrete type. — Every sample value for that key was null, so no concrete type could be inferred. Provide a sample with a real value for that field.
- A field uses Field(alias=...) with a renamed attribute. — That key is not a valid Python identifier or is a reserved word, so it is exposed under a safe attribute name with the original key kept as the alias.
- A list field came out as list[Any]. — The array in your sample was empty, so no element type could be inferred. Include at least one representative element to get a typed list.
Frequently asked questions
- Does it generate Pydantic v2 models?
- Yes. The output targets Pydantic v2 syntax: each model is a class that subclasses BaseModel, with annotated fields and Field(alias=...) where a JSON key is not a valid Python identifier.
- How are nested objects handled?
- Each nested object becomes its own BaseModel class, named in PascalCase from the owning key, and the parent references it by type. Classes are emitted in dependency order with the root model last.
- How does it map JSON types to Python types?
- Strings become str, whole numbers become int, decimals become float, booleans become bool, null produces Optional, arrays become list[...] with a unified element type, and objects become nested BaseModels.
- What happens with keys that aren't valid Python names?
- A key like first-name or 2fa, or a reserved word like class, is exposed under a safe snake_case attribute and the original key is preserved with Field(alias="...") so the model still parses your payload.
- How are arrays and empty arrays converted?
- Arrays become list[...] using the unified type of their elements, including a nested BaseModel for arrays of objects. An empty array has no element type to infer, so it becomes list[Any].
- Is my JSON sent anywhere when I generate Pydantic models?
- No. The JSON to Pydantic conversion runs entirely in your browser, so the data you paste is processed locally and never leaves your device.
Related tools
All ArrayKit tools