JSON Viewer Tool 100% client-side • No data is uploaded
Developer Blog

JSON to Python: dataclass vs TypedDict vs Pydantic

You have JSON and you want typed Python objects instead of raw dicts. There are three good targets — dataclass, TypedDict and Pydantic — and they solve different problems. Here is how to choose.

The shape

Every example below models the same tiny record:

{ "id": 1, "name": "Ada", "active": true }

dataclass — plain typed objects

from dataclasses import dataclass

@dataclass
class User:
    id: int
    name: str
    active: bool

You get attribute access (user.name) and clean equality, but no validation — you still parse the JSON yourself with User(**json.loads(s)). Best for internal data you already trust.

TypedDict — a dict with a known shape

from typing import TypedDict

class User(TypedDict):
    id: int
    name: str
    active: bool

It stays a plain dict at runtime (user["name"]) — the types exist only for the type-checker, with no runtime validation. Great when you want to keep the data as dicts but still get editor and mypy support.

Pydantic — validation at the boundary

from pydantic import BaseModel

class User(BaseModel):
    id: int
    name: str
    active: bool

user = User.model_validate_json(s)  # validates and may coerce compatible values
strict_user = User.model_validate_json(s, strict=True)  # reject type coercion

Pydantic parses and validates against the declared fields. Its default mode is intentionally permissive: compatible values such as "123" for an int may be converted, while values that cannot be validated raise a ValidationError. Pass strict=True, configure Pydantic strict mode on the model, or mark individual fields strict when exact input types matter. This makes Pydantic useful at boundaries such as API requests, configuration files, and webhooks, as long as you choose the coercion policy deliberately.

Generate the class automatically

Instead of typing the class out by hand, paste a JSON sample into the JSON to Python converter and it generates the class for whichever style you pick in your browser. Remove sensitive data before using online tools; see the privacy policy for analytics and third-party services.

Real JSON keys are not always valid Python identifiers. A key such as "2fa-enabled" needs a safe attribute plus an alias in Pydantic, while TypedDict's functional syntax preserves such keys directly. The converter handles those cases and also emits Union element types when a sample array contains more than one observed scalar type.

How to choose

  • Trusted internal data, want objects → dataclass.
  • Keep it as dicts, just want type hints → TypedDict.
  • Data crossing a trust boundary → Pydantic (validate once, trust after).

One caveat applies to every generator: it infers types from the examples it can see. The converter can form a Union from different values inside one mixed array, but a single object cannot reveal fields that are absent from that sample or whether null means optional. Read the output before you ship it, and test it against more than one representative payload.

Related articles