LazyTools

🔒 Every tool runs in your browser — the files and values you enter are never uploaded to any server. How it works

🐍 JSON to Python Dataclass Converter

Paste JSON and get Python @dataclass definitions with type hints — nested objects, lists and optional fields handled for you.

2 dataclasses generated

Rate this tool:
Anonymous — no account, no identifier

How the json to python dataclass converter works

The converter parses your JSON and emits Python @dataclass definitions with PEP 484 type hints. Whole numbers become int, decimals float, booleans bool, strings str, arrays List[...] and nested objects their own dataclass. Fields that are missing from some elements of an array of objects are typed Optional[...]. Classes are emitted deepest-first so each is defined before it is referenced, and the needed imports (dataclass, List, Optional, Any) are included.

This gives you typed models ready for json parsing, IDE autocompletion and static checkers like mypy in seconds. A couple of things you may refine by hand: a value with no decimals is typed int (widen to float if it can be fractional), and fields that can be null are marked Optional — but you may also want a default of None so the dataclass field is truly optional at construction. It is a scaffold to build on, not a substitute for knowing your data. Everything runs locally in your browser.

Frequently asked questions

How do I convert JSON to a Python dataclass?

Paste a JSON sample and the tool outputs @dataclass definitions with type hints. Set the root class name if you like, then copy the code into your project.

Does it add type hints?

Yes — each field is annotated (int, float, bool, str, List[...], or a nested dataclass), which powers editor autocompletion and type-checkers like mypy. Optional fields are typed Optional[...].

How does it handle nested objects?

Each nested object becomes its own @dataclass, defined before the class that references it, so the code is valid top-to-bottom. Arrays of objects are merged into a single element dataclass.

Why is a number typed as int?

Whole numbers infer to int and numbers with a decimal point to float, based on the sample. If a field can be fractional, widen it to float after generating — inference only sees the example you provide.

Is my JSON uploaded?

No — the conversion runs entirely in your browser and works offline, so your data never leaves your device.

Related developer tools

From the blog