JSON to Python Dataclass

Infer Python dataclasses from JSON.

FreeNo accountOn your device

Runs in your browser. How to verify

Loading tool…

About this tool

Paste a JSON object and get a Python dataclass with type hints on every field. Lists become List[T] and nested objects become dict[str, Any]. It also writes the dataclass and typing imports.

How to use

1
Step 1
Paste a sample JSON object.
2
Step 2
Press Run.
3
Step 3
Copy the class into your module and rename Root.

How it works

Each key becomes a field annotated with str, int, float, bool, List[T] (items merged, mixed types become Union) or dict[str, Any]. The output starts with the dataclasses and typing imports and a @dataclass decorator on a class named Root.

Worked example

A scored item

Input
{"id":1,"name":"Ada","score":9.5,"tags":["a"],"active":true}
Output
from dataclasses import dataclass from typing import Any, List, Union @dataclass class Root: id: int name: str score: float tags: List[str] active: bool
Walkthrough
Union is imported for mixed-type arrays; delete the import if you do not need it.

Limitations

  • Nested objects are dict[str, Any], not nested dataclasses.
  • Keys that are not valid Python identifiers (such as user-id) are not renamed.
  • No default values, Optional fields or from_dict method are generated.

Questions

How do I generate a Python dataclass from JSON?
Paste the object and press Run. Copy the dataclass into your code.
Does it create nested dataclasses?
No. Nested objects become dict[str, Any].
Which Python versions does the output need?
dict[str, Any] needs Python 3.9 or newer; List[...] works on 3.7+.
Can it load JSON into the dataclass?
It only writes the class. Use Root(**data) for flat objects.

Features

  • Type hints on every field
  • Imports included
  • List[T] from arrays
  • Runs in your browser

Related resources

Guides and videos from around the web—opens on YouTube or the publisher’s site.

  • What Is A RESTful API?

    Traversy Media

    Most APIs exchange JSON—context for pretty-print and schema checks.

    Watch on YouTube
  • Inspect Network Activity - Chrome DevTools 101

    Chrome for Developers

    Debugging payloads you paste into JSON and JWT tools.

    Watch on YouTube