JSON to Python Model

Paste JSON, get Pydantic models (or plain dataclasses). Keys that are already snake_case stay as they are; only illegal identifiers and Python keywords get an alias.

Nested models are emitted before the models that use them — Python resolves names in order, so a class has to exist before it can be referenced. null becomes Any, an empty array List[Any], and a field only some records carry gets Optional with a None default when you tick the box.

About JSON to Python Model

Paste JSON and get Pydantic models (or plain dataclasses): field names stay snake_case, types come from the values, and nested objects become their own model classes.

Referenced models are emitted first. Python resolves names in order — if Root appeared before Order, the List[Order] inside Root would not exist yet and you would get a NameError. So nested models are reversed to the front, which is the main difference from the Java page.

Aliases are rarely needed. snake_case is already Python convention, so a JSON key like user_id works as is; only keywords like class, or keys containing -, @, $, need Field(alias="..."). The result is a lot cleaner than the Java output.

Pydantic is the default because most people want it for FastAPI request bodies — it validates types, supports aliases, and gives you model_dump(). dataclass is there for plain data structures with no validation.

Three things it cannot infer: null becomes Any (no way to know what it should be), an empty array becomes List[Any] (nothing to look at), and a field only some records carry gets Optional[X] = None when you tick that box — otherwise it is required.

Frequently asked questions

Can I use the output as is?

You need pydantic v2. FastAPI projects already ship it; the dataclass style needs nothing beyond the standard library.

Why are field names mostly unchanged?

snake_case is already Python convention, so a key like user_id works directly. Only keywords (class, from) and keys with hyphens, @ or $ get renamed and aliased.

Pydantic or dataclass?

Use Pydantic when you need validation, FastAPI request bodies or aliases. Use dataclass when you just want to move data around.

Why are nested classes first?

Python resolves names in order — a class must exist before it can be referenced, otherwise you get a NameError.

How is optionality decided?

If a key is missing from some records in an array, the field is treated as optional. Tick the box and it becomes Optional[X] = None.

Is my JSON uploaded?

No. Everything runs in your browser.

Related tools

Converters View all ›