WebToolsPlanet
Converter Tools

JSON to Python Converter

Paste a JSON object and get Python classes — @dataclass, pydantic BaseModel, TypedDict, or attrs — with typed fields, nested classes, and snake_case naming.

Last updated: May 27, 2026

Client-Side Processing
Input Data Stays on Device
Instant Local Execution

Find this tool useful? Support the project to keep it free!

Buy me a coffee

What is JSON to Python Converter?

Modern Python has four mainstream ways to declare a data class: stdlib's `@dataclass`, the third-party `pydantic` (popular for runtime validation), `TypedDict` (purely a type hint, no runtime cost), and `attrs` (`@attrs.define`). Each fits a different use case — pydantic for API I/O, dataclass for value objects, TypedDict for typing existing dicts.

This tool generates the same field structure in any of those styles. It converts JSON keys to snake_case (Python convention), infers `int` vs `float` from the value, recognizes nested objects as nested classes, and uses `list[T]` (Python 3.10+) or `List[T]` (3.9 fallback) for arrays. The pydantic output adds `Field(alias="...")` when the JSON key cannot be a valid Python identifier.

How to Use JSON to Python Converter

1

Paste a JSON object (root must be an object).

2

Pick a style: @dataclass, pydantic, TypedDict, or @attrs.define.

3

Pick a Python version: 3.10+ for modern syntax or 3.9 for typing-module imports.

4

Toggle "Make all fields Optional" if any JSON key may be missing.

5

Copy the generated Python code into your project.

Common Use Cases

  • Bootstrapping pydantic models for a FastAPI service.
  • Generating @dataclass types for a Python CLI consuming JSON.
  • Producing TypedDict definitions to add types to existing dict-based code.
  • Translating an OpenAPI example into Python types.

Example Input and Output

A flat JSON object generates a @dataclass with snake_case fields.

JSON input
{ "userId": 42, "name": "Alice", "active": true }
Generated @dataclass
from dataclasses import dataclass


@dataclass
class Root:
    user_id: int
    name: str
    active: bool

Privacy

All conversion happens in your browser. No JSON is sent to a server.

Class ordering

Nested classes are emitted before the classes that reference them, so the file can be read top-to-bottom without forward references.

Python 3.9 typing imports

On Python 3.9 the tool emits List[T] and Optional[X] from the typing module. On 3.10+ it uses the built-in list[T] and X | None which need no imports.

Frequently Asked Questions

@dataclass vs pydantic — which should I pick?
Use pydantic when you need runtime validation, JSON parsing, and OpenAPI integration (e.g. FastAPI). Use @dataclass when you just need a typed container with no validation overhead.
Why TypedDict?
TypedDict is purely a typing construct — no runtime cost, no instance methods. Use it when you have existing code that passes around dicts and you want type-checker support without refactoring.
How are int vs float distinguished?
Number.isInteger checks the input: a whole number becomes int, a decimal becomes float. For decimal.Decimal precision, change manually after generation.
What happens to JSON keys with hyphens or spaces?
Snake_case conversion turns them into valid Python identifiers. For pydantic, the original key is preserved via Field(alias=...).
Does this send my JSON anywhere?
No. All generation happens locally in your browser.