Format Code Now
/

JSON to Python Dataclass Converter

FV-15

Paste a JSON sample and press Convert to get Python type definitions as dataclasses, TypedDicts or Pydantic models. Keys that are not valid Python names are handled for you. It runs in your browser.

Runs in your browser. Nothing is uploaded.

JSON

Python

How to use it

  1. Paste a JSON sample into the input box, or open a .json file.
  2. Choose dataclass, TypedDict or Pydantic BaseModel.
  3. Press Convert and copy the result into a .py file, or download it.
  4. Load your data with Root(**data), a plain dict typed as Root, or Root.model_validate(data) for Pydantic.

Three styles, one inferred shape

All three styles come from the same inference. A dataclass gives you a lightweight class, a TypedDict describes a plain dict without changing how you read it, and a Pydantic model validates the data when you build it. Types are written as list[...] and Optional[...]. Nested objects become their own classes, and they are written before the class that uses them so the file runs as pasted.

Keys that are not Python names

A key such as first-name, class or 2fa cannot be a field name. A TypedDict switches to the functional form, which accepts any key exactly. Pydantic renames the field and adds Field(alias="first-name"), with populate_by_name so either name works. A dataclass has no alias feature, so the field is renamed and a comment shows the original key. You then map it yourself when you load the data.

Optional keys and ordering

A key that is null in some items becomes Optional[T]. A key missing from some objects becomes Optional[T] = None in a dataclass or Pydantic model, and NotRequired[T] in a TypedDict, which needs Python 3.11 or typing_extensions. Python dataclasses require fields without defaults to come first, so those optional fields are moved to the end and the field order can differ from the JSON.

Questions people ask

How do I turn JSON into a Python dataclass?

Paste the JSON, keep the dataclass style, and press Convert. Copy the generated classes into a .py file. Note that Root(**data) fills only the top level: nested dicts stay dicts unless you build the nested dataclasses yourself.

Which style should I pick?

Use Pydantic when you want validation and conversion of nested data. Use TypedDict when you keep working with plain dicts and want type hints. Use dataclass for simple typed records you create or fill in code.

Does the Pydantic output work with Pydantic 1?

It is written for Pydantic 2: it uses ConfigDict and populate_by_name. Pydantic 1 used a different configuration style, so rename those parts if you are still on version 1.

What happens to a key that is a Python keyword?

A key such as class gets a trailing underscore as the field name, class_. Pydantic adds an alias for the original key, a TypedDict uses the functional form with the exact key, and a dataclass notes the key in a comment.

Why are large integers not a problem here?

Python integers have no fixed size, so every whole number in the JSON is written as int, however many digits it has. Decimal numbers become float, which is a 64-bit float and can round very long decimals.

What do mixed types and empty arrays become?

A key that holds different types in different items, a key that is always null and the items of an empty array all become Any. Add a more precise type by hand when you know what the data should hold.

About this tool

Infers types from the sample you paste, so it only knows what that sample shows. Keys that are valid Python names are used as they are, so Root(**data) and plain dict access keep working. A key that is not usable as a name (first-name, a keyword such as class, a leading digit) is handled per style: a TypedDict switches to the functional form with the exact key, Pydantic renames the field and adds Field(alias=...) with populate_by_name, and a dataclass renames the field and notes the original key in a comment, without mapping it for you. Types are written as list[...] and Optional[...]. In a dataclass, fields without a default come first and keys missing from some objects follow with = None, so field order can differ from the JSON. In a TypedDict a missing key is NotRequired, which needs Python 3.11. Integers stay int, whatever their size. Mixed types, null-only keys and empty arrays become Any. Nested classes are written before the class that uses them.

Format Code Now: free, no ads, no accounts. Your input stays in your browser.