JSON to Python — Generate Python Dataclass from JSON
Generate Python dataclass from JSON with type hints. Optional fields, snake_case names, Pydantic support — in your browser, no upload.
JSON to Python Dataclass
Convert JSON to Python dataclass with type hints. Works entirely in your browser.
Python’s type annotation system has grown from a niche feature used by linters into a central part of the language. Dataclasses, TypedDicts, and Pydantic models are three different answers to the same question: how do I represent structured data in Python with type safety, without writing a hundred lines of boilerplate per class? The JSON-to-Python generator walks a JSON response and emits a typed Python class that knows its own shape — which is the first step toward type-safe API consumption, configuration management, and data validation.
The non-obvious choice in Python data modeling is between runtime validation and type-checker validation. A Pydantic model validates at runtime — if you pass a string where an int is expected, it raises a ValidationError immediately, which makes it the right choice for API boundaries (FastAPI endpoints, message queues, config file parsing). A dataclass validates only in the type-checker (mypy, pyright) — if you pass a wrong type, your IDE flags it but the code still runs. A TypedDict does nothing at runtime and is purely a type-checker annotation. Choose Pydantic for data that arrives from an external source. Choose dataclass for data that starts inside your code and needs to be passed around with type safety. Choose TypedDict for codebases where the type-checker is the enforcement layer.
The most common downstream step after generating a dataclass is to pass it to json.loads() or requests.get().json() with a custom decoder that maps JSON keys to dataclass fields. For FastAPI, wrap the generated Pydantic model in a route parameter and FastAPI handles validation and serialization automatically. For SQLAlchemy, the generated dataclass becomes the attributes of an ORM model — add the table mapping by hand. The generated class is the schema. The wiring is the application.
How to use
Paste your JSON
Drop a JSON object or paste a JSON string. The generator infers Python types for each field and emits a `@dataclass` with type hints for every attribute.
Choose class style
Pick between `@dataclass` (standard library), `TypedDict` (type-checking only, no runtime), and `pydantic.BaseModel` (full validation with Pydantic). Each style has different intents.
Copy or download the .py file
Copy the Python class to your clipboard or download as a .py file. The output runs with Python 3.9+ and the standard library — no extra dependencies for the dataclass option.
Frequently asked
Should I choose dataclass, TypedDict, or Pydantic?
Dataclasses are the default — they have type hints, work with `dataclasses.asdict(dc)` for round-tripping, and require no extra packages. TypedDicts are type-checker-only constructs (mypy, pyright) with zero runtime overhead. Pydantic models add validation, parsing, and schema generation but require the `pydantic` package.
How are camelCase JSON keys handled?
By default, JSON keys in camelCase (`userId`, `createdAt`) are emitted as Python `@dataclass` fields in snake_case (`user_id`, `created_at`) with a `field(metadata={'json': 'userId'})` annotation. Toggle 'keep original case' to emit the fields exactly as they appear in the JSON.
How are optional fields handled?
Fields that are null in the sample and fields that are absent in some array elements both become `Optional[T]` with a default of `None`. The type hint `Optional[str]` is the Python equivalent of `string | null` in JSON.
What about nested JSON objects?
Nested objects become nested dataclass or Pydantic model classes. For TypedDict mode, nested objects become nested TypedDicts. The nesting mirrors the JSON structure exactly — a `user.profile.name` path produces a `User` class with a `profile: Profile` field.
Does it handle Python reserved words?
Yes. A JSON key like `class`, `import`, `from`, `pass`, or `type` is emitted as `class_`, `import_`, `from_`, `pass_`, or `type_` in the Python class. The `metadata={'json': 'class'}` annotation preserves the original key for serialization.
Limitations
- No async or FastAPI integrationThe output is a data class only. FastAPI route definitions, async database mappings, and dependency injection wiring are not generated — add them by hand around the dataclass.
- Inferred types use Any for ambiguous casesA JSON array of mixed types or a deeply polymorphic value is typed as `Any`. Add a narrower type hint by hand if the API guarantee is stronger than the sample suggests.
- Pydantic V2 vs V1The Pydantic option emits Pydantic V2-style models (`model_validate`, `model_dump`). For Pydantic V1 codebases, change the method calls to `.parse_obj` and `.dict` by hand.
Platform notes
- macOS
- PyCharm, VS Code with Pylance, and vim with jedi all handle the generated dataclass. Use the browser tool for one-off generation from an API response during development.
- Windows
- The generated dataclass works in any Python environment. Use the browser tool for quick one-off generation of a dataclass from content pasted from an API explorer.
- Linux
- For command-line work, `datamodel-code-generator` is the standard equivalent. The browser tool is the right pick for one-off generation where a Python environment with optional packages is not available.
- Web
- Runs entirely client-side. The generation takes milliseconds — paste, copy, done.