Turn JSON into Pydantic models
Paste or drop JSON and get Pydantic v2 classes, with optional and nullable fields read from every value and camelCase keys aliased.
- Free, no sign-up
- Nothing uploaded
- Pydantic v2 .py
or drop a .json file · converted as you type
No JSON handy? Try one:
How it works
- Paste or drop the JSON. An API response, a list of records, a webhook payload or a JSON Lines log. Comments and trailing commas are removed first; anything else that is not JSON gets the line and column where it breaks.
- Every value is read. Each object becomes a class named after its key (
itemsbecomesItem). In a list, a field some items lack gets= None; a field that is sometimesnullbecomesstr | None. Objects with the same fields under the same name share one class, and every class is written before the class that uses it. - Python-safe names.
userIdbecomesuser_idwithalias="userId";from,class,json,2faandX-Mailerget safe names too. Text that is a UUID, date, date-time, e-mail or web address in every value getsUUID,date,datetime,EmailStrorHttpUrl. - Models or API models. The API view splits the record into
Base,Create,Update(every field optional, for PATCH) andResponse(adds the id and timestamps the server sets). Download the.pyfile or copy it.
Questions
- Is my JSON uploaded?
- No. The JSON is read inside your browser tab and the Python is written there too. Only the optional AI step sends anything: the generated models and a summary of each field (its name, type and counts and, if you leave the box ticked, up to five example values), and only when you press its button.
- How does it decide that a field is optional?
- In a list of objects, a field that some items have and others lack gets a default:
field: int | None = None. A field that is always there but sometimes null gets| Nonewith no default, so it stays required. The page counts every item, so paste a sample that shows the variety your API really returns. - What happens to camelCase keys and keys like from or class?
- With snake_case on,
userIdbecomesuser_id = Field(alias="userId")and the class getsmodel_config = ConfigDict(populate_by_name=True), so it reads the JSON keys and also accepts the Python names. Python keywords, names Pydantic reserves (json,copy,schema), names the file uses as types (date,list), keys that start with a digit or underscore and keys with dashes or spaces get a safe name and an alias. Write the JSON back withmodel_dump(by_alias=True). - What are the API models?
- The pattern most FastAPI projects use: a Base model with the fields a client sends, a Create model for the POST body, an Update model where every field is optional for PATCH, and a Response model that adds the fields the server sets, such as the id and the created and updated times. The page guesses those from the field names; the AI step can pick them for you.
- Which Pydantic and Python versions does it write for?
- Pydantic v2. The default syntax (
int | None,list[str]) needs Python 3.10 or newer; under More options you can switch toOptional[int]andList[str], which works on Python 3.8 and 3.9.EmailStrneeds the email-validator package (pip install "pydantic[email]"). - A field is always null or a list is always empty. What type does it get?
Noneandlist[Any], because the JSON does not show anything else. The page lists these fields so you can set the real type by hand, or paste a sample where they have values.