JSON to Python
Generate Python dataclasses or pydantic models from a JSON sample: nested objects, List and Union types, Optional fields and four naming conventions.
Runs in your browserEvery computation happens in your browser — your data never leaves this device.
from dataclasses import dataclass
from typing import Any, List, Optional, Union
@dataclass
class RootOwner:
name: str
email: Any
@dataclass
class RootItems:
sku: str
price: float
note: Optional[str]
@dataclass
class Root:
id: int
name: str
is_active: bool
score: float
owner: RootOwner
tags: List[str]
items: List[RootItems]
meta: AnySummary: Types 3 · Fields 13 · Name clashes 0
What this tool does
- Turn a sample API response into annotated Python: dataclasses by default, pydantic BaseModel when you tick the option, ready to paste into a project.
- Add type hints to a scraper or data script: nested objects become their own classes, arrays merge into List[T] and mixed arrays become Union[...].
- Validate request bodies with pydantic: in pydantic mode nullable and optional fields get = None defaults so missing keys stop raising ValidationError.
- Keys with dashes or a leading digit are turned into legal identifiers and Python keywords (class, import, …) get an underscore, so the generated module always imports.
Example
Input
{"id":1,"name":"UniKit","tags":["a","b"]}Output
from dataclasses import dataclass
from typing import Any, List, Optional, Union
@dataclass
class Root:
id: int
name: str
tags: List[str]Dataclasses get no field defaults (Python requires defaulted fields to come last); switch to pydantic mode when you want defaults, where optional fields get = None.
Frequently asked questions
Should I use dataclass or pydantic?
If you only need types for your IDE and type checker, dataclasses are enough and add no dependency. Choose pydantic when you want runtime validation of external data, field defaults, or serialization helpers.
Why do optional dataclass fields have no = None?
Python requires fields with defaults to come after fields without them, but JSON key order is arbitrary, so adding defaults would produce "non-default argument follows default argument". Dataclasses therefore only carry annotations; use pydantic when you need defaults.
What do null values and missing keys generate?
A null value or a key that only appears in some array elements is wrapped in Optional (Optional[int], say), and nullable array items become List[Optional[T]]. One exception: a key that is only ever null across the sample carries no type information and degrades to Any.
Do nested objects become nested classes?
No. Every object becomes a top-level class named from its field path (an object under owner is called Owner) with the root class last, which naturally satisfies Python's define-before-use rule.
Is my JSON uploaded anywhere?
No. Everything is parsed and generated locally in your browser; there are no network requests and it works offline.
Keywords:jsonpythondataclasspydantictypingjson to pythonJSON 转 Python数据类类型注解代码生成