Python models from a JSON sample
Paste a response from an API and get Python classes that describe it: one model per object, nested ones named after their keys, optional fields where a key is missing from some samples, and int or float worked out from the values. camelCase keys become snake_case fields with an alias, so the model reads the JSON as it is.
A sample shows what was sent, not everything that can be sent. Treat the result as a first draft and check optional fields and unions against the API's documentation.
- Pydantic v2Validates and converts on load; aliases map JSON keys to Python names.
- DataclassPlain Python with no validation; you map keys to fields yourself.
- TypedDictTypes the dict as it is, so keys stay exactly as in the JSON.
- Optional or None
X | None = Nonemay be missing;X | Nonemust be present but may be null.
How to use the converter
- Paste a JSON sample — or an array of several, to merge them.
- Pick the output: Pydantic v2, Dataclass or TypedDict.
- Keep Snake Case Fields on for Pydantic to get Python names with aliases; Detect Dates turns ISO strings into
datetime. - Read the notes: renamed keys, merged samples and empty arrays are called out.
- Copy the code, then load data with
Order.model_validate_json(text).
Examples
Model a camelCase API
unitPrice becomes unit_price with an alias, so both names work.
Merge several records
The color only one record has becomes optional.
Handle keywords and odd keys
class, from and 2fa get valid names and keep their JSON keys.
Type a dict without converting it
A TypedDict keeps the keys exactly as they arrive.
Loading data with the model
# Validate a JSON string or an already-parsed dict order = Order.model_validate_json(response.text) order = Order.model_validate(response.json()) # Back to JSON with the original camelCase keys order.model_dump_json(by_alias=True)
Frequently asked questions
Pydantic v1 or v2?
The models use Pydantic v2: ConfigDict, model_validate and X | None types. v1 used an inner class Config and parse_obj; new code should use v2.
Why are my fields renamed?
Python names cannot contain dashes, start with a digit or be keywords such as class. The field gets a valid name, and Field(alias=…) keeps reading the original key. populate_by_name=True lets you use either name.
Why float when most values were integers?
If any sample has a decimal, the field must hold it, so it becomes float. Pydantic still accepts whole numbers for a float field.
Why do dataclasses warn about renamed keys?
Dataclasses have no aliases. A key such as first-name cannot be a field name, so you have to map it when you build the object — or use Pydantic or a TypedDict instead.
Is my JSON sent anywhere?
No. The models are generated in your browser; check the Network tab in your developer tools.