Skip to content
XUtilHub

JSON to Python – Dataclass, Pydantic & TypedDict

Convert JSON to Python dataclasses with from_dict, Pydantic v2 models with aliases, or TypedDicts. Snake_case names, optional fields and nested models included.

  • Free, no sign-up
  • Runs in your browser, nothing uploaded
Input
Code

About the JSON to Python

Turn a JSON sample into Python models: dataclasses with a from_dict() constructor, Pydantic v2 models with field aliases, or TypedDict definitions. Names are converted to snake_case and Python keywords are handled safely.

Key features

  • dataclasses with a from_dict() classmethod
  • Pydantic v2 models with Field aliases
  • TypedDict definitions with NotRequired for optional keys
  • snake_case attribute names; Python keywords made safe
  • Optional[...] for nullable values
  • Classes ordered so every reference is defined first

Python models from JSON in one click

Working with JSON as nested dictionaries means typos, KeyErrors and no autocomplete. Generate typed Python models instead: dataclasses for zero-dependency code, Pydantic v2 for validation, or TypedDicts to keep using dicts with type hints.

Example

{ "userId": 7, "class": "admin", "address": { "city": "London" }, "manager": null }

dataclass (default):

@dataclass
class Root:
    user_id: int
    class_: str
    address: Address
    manager: Any = None

    @classmethod
    def from_dict(cls, d: dict[str, Any]) -> Root:
        return cls(
            user_id=d["userId"],
            class_=d["class"],
            address=Address.from_dict(d["address"]),
            manager=d["manager"],
        )

Pydantic v2:

class Root(BaseModel):
    model_config = ConfigDict(populate_by_name=True)

    user_id: int = Field(alias="userId")
    class_: str = Field(alias="class")
    address: Address
    manager: Any  # present but null in the sample, so still required

root = Root.model_validate(data)

Choosing a style

dataclassPydantic v2TypedDict
DependenciesNonepydanticNone
Runtime validation❌✅❌
Works with plain dicts❌❌✅
Nested parsingfrom_dict()automaticn/a
Best forScripts, internal modelsAPIs, FastAPI, settingsType-hinting existing dict code

Tips

  • Provide several array items so optional keys get None defaults.
  • Use with FastAPI: Pydantic models work directly as request and response models.
  • Type-check with mypy or pyright for the most value.

Generating for other back ends? See JSON to Go, JSON to C# or JSON to Java.

How to use the JSON to Python

  1. Paste your data into the input box, upload a file, or click Sample to try it out.
  2. Adjust the options above the editor if you need to (indentation, delimiter, and so on).
  3. The Python appears instantly in the output panel. Any errors are shown with their line and column.
  4. Click Copy to copy the result, or Download to save it as a file.

Frequently asked questions

dataclass, Pydantic or TypedDict?

Use Pydantic for API input and anything that must be validated. Use dataclasses for lightweight models without dependencies. Use TypedDict when you want type hints but keep working with plain dicts.

How are keys like 'firstName' or 'class' handled?

Attributes are converted to snake_case (first_name) and Python keywords get a trailing underscore (class_). dataclass from_dict and Pydantic aliases map them back to the original keys.

Which Python version is required?

Python 3.9+ for list[...] annotations. TypedDict output uses typing.NotRequired, which needs Python 3.11+ (or typing_extensions on older versions). Pydantic output targets Pydantic v2.

Can Pydantic models serialize back to the original keys?

Yes. Call model.model_dump(by_alias=True) or model_dump_json(by_alias=True) to get the original JSON keys.

Is my JSON uploaded to a server?

No. All JSON tools run entirely in your browser, so your data never leaves your device.

Is there a size limit?

There is no fixed limit. Files of several megabytes work fine; very large files depend on your device's memory.

Do I need an account?

No. Every JSON tool is free and works without signing up.

Last updated:

Related JSON tools