Tools

JSON to Python Class

Generate Python dataclasses or Pydantic models from JSON

How to convert JSON to Python classes

  1. Paste a sample JSON object or array.
  2. Choose Pydantic model (selected by default) for validation and parsing, or Python dataclass for plain standard-library classes.
  3. Set the root class name and copy the generated code.
  4. With Pydantic, load data with Root.model_validate(data) or Root.model_validate_json(text).

What you get

Each nested object becomes its own class, named in PascalCase after its key, and lists become List[...]. Field names are converted to snake_case. Where that changes the name, for example userId to user_id, Pydantic models keep the original key with Field(alias="userId"), so parsing still works. Fields that are missing in some items or can be null become Optional[...] with a default of None.

For dataclasses, fields with defaults are placed after required ones, because Python requires that order. Classes are emitted before they are used, and from __future__ import annotations keeps forward references safe. Integers and floats are distinguished based on your sample.

Pydantic is used by FastAPI, LangChain and many data pipelines, so this is a quick way to model an API response or LLM output. The conversion happens in your browser.

FAQ

Pydantic or dataclass: which should I use?

Use Pydantic when you parse external data, as it validates types and converts values. Use dataclasses for simple internal structures with no dependency.

Does this work with Pydantic v2?

Yes. The models use BaseModel and Field, which work in both v1 and v2. With v2, use model_validate instead of parse_obj.

How do I populate fields by their Python name?

Add model_config = ConfigDict(populate_by_name=True) to the model so both the alias and the snake_case name are accepted.