Home JSON to Pydantic

JSON to Pydantic model generator

Paste a JSON array and get the Pydantic models that parse it — nested models, Optional fields, and aliases wherever a JSON key is not a legal Python attribute name.

Other languages:TürkçeDeutsch中文हिन्दीРусский

What the full sample tells you

Inference reads every record you paste. Include records with absent keys, nulls, mixed numbers and empty arrays so the generated types describe those cases.

  • Missing keys become optional. A key absent in any record can be omitted.
  • Null values remain nullable. A key can be both optional and nullable.
  • Integers and decimals share a number type. Target-specific output may distinguish them only when all observed values allow it.
  • String shapes are recognized where supported. Targets can preserve UUID, date, timestamp, email and URL annotations.
  • Empty arrays leave their element type unknown. Add representative elements before relying on the result.

The result describes the sample. Review it against your API contract before using it as a type or validator.

JSON keys can contain spaces, dots and hyphens; Python attributes cannot. A key like "full name" becomes an attribute named full_name with an alias back to the original, so parsing the untouched payload still works:

class User(BaseModel):
    full_name: str = Field(alias="full name")

Populate by alias when constructing from JSON — model_config = ConfigDict(populate_by_name=True) in Pydantic v2 if you also want to construct by the Python name. Without the alias the field would simply never populate, and it would do so silently.

Imports are emitted only where used

Optional, List, Union, UUID, date, datetime and Field each appear in the import block only if the models below actually use them. An unused import is a lint error in most Python projects, which turns a generated file into something you have to edit before it passes CI — and a generator whose output needs editing is one people stop using.

Model order and forward references

Nested models are emitted before the models that annotate fields with them. A class annotation naming a class defined further down the file is a NameError at import time, so the ordering is correctness rather than preference.

What is deliberately left to you

  • EmailStr is not used. It requires the email-validator package; the generated file has no dependencies beyond Pydantic itself. Swap it in if you already have that installed.
  • No validators. Ranges, regexes and cross-field rules are business logic, and a sample cannot see them.
  • No enums. A field holding only two values in the sample may hold a third tomorrow.
  • Pydantic v2 syntax. Optional[X] with Field(default=None) works on v1 too, but model_config and the v2 methods do not exist on v1.

Common questions

Does this target Pydantic v1 or v2?

The generated models use syntax valid in both: Optional[X] with Field(default=None), aliases via Field(alias=...), and standard typing constructs. Configuration you add on top — model_config versus class Config — differs between the versions.

Why is a JSON key with a space turned into an attribute with an underscore?

Because a Python attribute cannot contain a space. The field gets an alias back to the original key so parsing the untouched payload still works — without it the field would silently never populate.

Why is EmailStr not used for email fields?

EmailStr requires the email-validator package. The generated file is meant to run against Pydantic alone; swap str for EmailStr if you already depend on it.

Why are nested models defined first?

A class annotation that names a class defined further down the file raises a NameError when the module is imported. Emission order is correctness, not style.

Are unused imports included?

No. Optional, List, Union, UUID, date, datetime and Field are each imported only if the models use them, so the output does not fail a lint run before you have touched it.

Last updated