Generate Python dataclasses, TypedDict, or Pydantic models from JSON data instantly. Free online tool — no signup, your data stays in your browser.
Convert JSON to Python →Given this JSON:
{
"id": 101,
"name": "Alice Johnson",
"email": "alice@example.com",
"active": true,
"score": 9.5
}
from dataclasses import dataclass
@dataclass
class Root:
id: int
name: str
email: str
active: bool
score: float
from typing import TypedDict
class Root(TypedDict):
id: int
name: str
email: str
active: bool
score: float
from pydantic import BaseModel
class Root(BaseModel):
id: int
name: str
email: str
active: bool
score: float
# Usage:
user = Root.model_validate(json_dict)
print(user.name) # "Alice Johnson"
print(user.model_dump_json()) # Back to JSON
| JSON Type | Python Type |
|---|---|
string | str |
integer (42) | int |
float (3.14) | float |
boolean | bool |
null | None / Optional[T] |
| array of strings | list[str] |
| array of objects | list[ChildClass] |
| object | Nested class |
| mixed array | list[str | int] (union) |
import json
from dataclasses import dataclass
@dataclass
class User:
id: int
name: str
email: str
active: bool
def from_json(json_str: str) -> User:
data = json.loads(json_str)
return User(**data)
user = from_json('{"id":1,"name":"Alice","email":"a@b.com","active":true}')
print(user.name) # Alice
from pydantic import BaseModel, EmailStr, ValidationError
class User(BaseModel):
id: int
name: str
email: EmailStr
active: bool = True # default value
# Valid JSON
user = User.model_validate_json('{"id":1,"name":"Alice","email":"alice@example.com"}')
print(user.id) # 1
print(user.email) # alice@example.com
# Invalid JSON — Pydantic raises ValidationError with clear messages
try:
User.model_validate_json('{"id":"not-a-number","name":"Alice"}')
except ValidationError as e:
print(e) # id: Input should be a valid integer
| Format | Best For |
|---|---|
| TypedDict | Type checking only, stays as dict, easiest JSON round-trip |
| Dataclass | Structured data with methods, no external dependencies |
| Pydantic | API validation, FastAPI, automatic JSON serialization, strict typing |
| Plain dict | Quick scripts, prototyping, when structure doesn't matter |
null maps to Python None. In type annotations, a nullable field is typed as Optional[str] (or str | None in Python 3.10+). Fields that are always null in the sample data are typed as Optional[Any].dataclass when you want an actual class with methods and a constructor. Use TypedDict when you only need type annotations for dicts (no instance creation, useful with mypy). For simple API response typing, TypedDict is lighter-weight.json module: json.loads(json_string) for strings, or json.load(file_obj) for files. For type-safe validation and parsing, use Pydantic: from pydantic import BaseModel; user = User.model_validate_json(json_str).User dataclass and an Address dataclass.json.dumps(data). For dataclasses, use json.dumps(dataclasses.asdict(obj)). For Pydantic models, use model.model_dump_json(). For custom objects, pass a custom default serializer function to json.dumps().Free, instant — choose dataclass, TypedDict, or Pydantic.
Open JSON to Python →Also useful: JSON to TypeScript | JSON to Java | JSON Schema Tutorial | JSON Validator | Validate JSON