JSON to Python Class

Dataclasses, Pydantic models or TypedDicts generated from a JSON sample.

About this tool

Paste a JSON sample, choose dataclasses, Pydantic v2 or TypedDict, and set the root class name. Typed classes appear as you type, with imports included and nested classes defined before the classes that use them. Copy them into your project.

Choosing dataclasses, Pydantic or TypedDict

All three describe the shape of JSON data with Python type hints, but they behave differently. Dataclasses come with the standard library. They give you real objects with attributes and a readable repr, but they don't validate or convert nested dictionaries by themselves. Pydantic models parse and validate: User.model_validate(data) checks types, converts nested objects into model instances and reports clear errors. Most FastAPI and LLM tooling uses Pydantic. TypedDicts are plain dictionaries with type hints, which suits code that already passes dicts around and only needs checking by mypy or Pyright. The generator merges objects inside arrays. A key that only some items have becomes optional (= None, or NotRequired[...] in a TypedDict), and a value that's sometimes null gets | None. camelCase keys such as orderId become snake_case attributes like order_id. In Pydantic, Field(alias="orderId") keeps parsing working, while TypedDicts keep the exact JSON keys, because their keys must match. Keys that aren't valid identifiers switch the TypedDict to its functional syntax. Honest limits: the output uses Python 3.10+ syntax (list[str], str | None), NotRequired needs 3.11+, and types reflect your sample, not the API's guarantees. JSON to Go, JSON to TypeScript and the JSON Schema generator are related.

Frequently asked questions

How do I convert JSON to a Python dataclass?

Paste the JSON and choose dataclasses. Each object becomes a @dataclass with typed fields, and optional fields come last with = None.

Should I use Pydantic or dataclasses?

Use Pydantic when you parse untrusted or external JSON and want validation. Use dataclasses for simple internal data with no dependencies.

How are camelCase keys handled?

Attributes become snake_case. Pydantic adds Field(alias=...) so the original keys still parse, and dataclasses note the JSON key in a comment.

Is my JSON uploaded?

No. Everything runs in your browser and nothing is stored or sent.