Utilo

JSON to Python dataclass Converter – Free, Online, Private

Generate Python dataclasses from a JSON sample, with list[...] annotations and None defaults where a value was missing.

Input
Output

How to convert JSON to Python dataclass

  1. Paste a JSON object or array of objects.
  2. Copy the dataclasses. Nested objects are their own classes.
  3. Load real JSON with a small helper, or switch to Pydantic if you want validation.

Why dataclasses and not dicts

A dict preserves JSON with no work, and it also preserves every typo. A dataclass names the fields you saw and gives them annotations a type checker can read. This page emits @dataclass classes, not Pydantic models and not TypedDict. The standard library is enough to import them. Field names are snake_case. When the JSON key was already snake_case it is kept. When the key was camelCase, the Python name is converted and a trailing comment records the original key so you can write a loader. Values use builtin generics: list[str], not typing.List, because current Python accepts the builtin form.

Types

JSON strings, booleans, integers and floats become str, bool, int and float. A number with a fractional part anywhere in a merged field becomes float. Null in the sample makes the annotation include None. Arrays become list[element]. Objects become classes. A union appears when the same field holds two kinds of value across array items, printed with the | operator. Any is the fallback for an empty list. There is no runtime validation: a dataclass will accept a wrong type unless you add a checker. That is the trade for a dependency-free snippet.

Worked example

The sample prints a Root dataclass with id: int, name: str, active: bool, tags: list[str] and address: Address. Address is another dataclass with city and zip. You can construct Root(42, "Ada Lovelace", True, ["math"], Address("London", "SW1A")). Building one from json.loads is a few lines: walk the dict and pass address=Address(**data["address"]). If you need that loader generated too, the classes are still the part that takes the time to get right. Rename Root to User after you paste.

Limits

Python identifiers cannot contain hyphens, so those keys are converted and commented. A key that collides with a keyword such as class gets a trailing underscore. Dates stay str. Decimal money should not become float; if the sample used a JSON number for money, change the annotation to Decimal yourself. The page does not import dataclasses.json or mashumaro. Generation is local, which is what you want when the sample is a production payload pulled from logs.

Frequently asked questions

Does this emit Pydantic?

No. The classes are dataclasses. The JSON to Types tool elsewhere on Utilo can emit Pydantic if you want validators.

How do I parse JSON into the class?

json.loads gives a dict. Pass the fields into the constructor, or write a from_dict that builds nested dataclasses. The snippet is the types.

Which Python version?

Annotations use list[str] and int | None, which match Python 3.10 and newer. On 3.9, quote them or import annotations from __future__.

Is my sample uploaded?

No. The classes are built in your browser.