Pydantic model from JSON
When you parse JSON from an API, an LLM’s structured output, or a config file,
a Pydantic model gives you validation, type hints, and editor autocompletion for
free. Writing those models by hand for nested data is tedious. This tool takes a
JSON sample and generates a complete set of Pydantic v2 BaseModel classes —
one for the root and one for each nested object — with correct types and
Optional annotations.
How it works
The generator walks your parsed JSON and maps each value to a Python type:
strings to str, integers to int, floats to float, booleans to bool,
nulls to Optional[...] = None, and arrays to list[...] based on the first
element. Whenever it encounters a nested object, it creates a new BaseModel
class named after the field and references it by name. Classes are emitted in
dependency order — innermost models first — so the resulting file is valid
Python you can run as-is. Arrays of objects merge their keys so fields absent
from some items are correctly marked optional.
Tips and notes
- Complete samples infer better. Optionality and types come from the values present, so a fully populated example produces the most accurate model.
- Arrays of objects help. Passing several objects lets the tool detect which fields are sometimes missing or null.
- Add validators after. The output is the structural skeleton — layer on field validators, constraints, and aliases once the shape is right.
- Local only. Your JSON never leaves the browser.