JSON to Pydantic
← SkillSafe

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.

or drop a .json file · converted as you type
No JSON handy? Try one:

How it works

  1. 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.
  2. Every value is read. Each object becomes a class named after its key (items becomes Item). In a list, a field some items lack gets = None; a field that is sometimes null becomes str | None. Objects with the same fields under the same name share one class, and every class is written before the class that uses it.
  3. Python-safe names. userId becomes user_id with alias="userId"; from, class, json, 2fa and X-Mailer get safe names too. Text that is a UUID, date, date-time, e-mail or web address in every value gets UUID, date, datetime, EmailStr or HttpUrl.
  4. Models or API models. The API view splits the record into Base, Create, Update (every field optional, for PATCH) and Response (adds the id and timestamps the server sets). Download the .py file 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 | None with 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, userId becomes user_id = Field(alias="userId") and the class gets model_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 with model_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 to Optional[int] and List[str], which works on Python 3.8 and 3.9. EmailStr needs the email-validator package (pip install "pydantic[email]").
A field is always null or a list is always empty. What type does it get?
None and list[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.