Shard Tools

JSON to Python Dataclass

Generate Python dataclasses from a JSON sample in your browser, with type hints.

Maintained by Roshan.

Use JSON to Python Dataclass

Converts json sample to python dataclass in this tab, with nothing sent anywhere.

Runs entirely on your deviceThe conversion happens in this browser tab. Nothing you type is uploaded, logged or stored, which is worth knowing for anything you would not paste into a search box.

Explore generators

Find related browser-local tools for nearby tasks without starting another search.

Browse all generators

About JSON to Python Dataclass

A Python dataclass turns a shapeless dictionary of JSON into a typed object with named fields, and generating one from a sample is the quick way to get there. Paste the JSON, and get a dataclass with type hints already filled in - snake_case field names, the right primitive types, and nested dataclasses for nested objects. This is genuinely useful when you are wrapping an API or loading structured data and want editor autocompletion and a clear shape instead of dictionary key lookups scattered through the code. The inference is conservative: it types what the sample shows, converts keys to Python's snake_case convention, and imports the typing helpers the output needs, so the result runs as written and can be refined from there.

Using JSON to Python Dataclass

  1. The JSON is parsed and inferred recursively into a set of dataclasses.
  2. Field names are converted to snake_case, Python's convention, from the original JSON keys.
  3. Nested objects become their own dataclasses, referenced by the parent.
  4. Type hints map JSON primitives to Python types; arrays become typed Lists.
  5. The needed imports - dataclass and typing helpers - are included at the top so the output runs.

Where json to python dataclass helps

Wrapping an API response

Paste a sample and get dataclasses with type hints, so the response becomes a typed object with autocompletion.

Loading structured data

Generate the classes from an example record, then narrow numeric types and mark optional fields.

Before you use the result

Questions about JSON to Python Dataclass

Does it use type hints?

Yes. Every field is annotated, and the necessary typing imports are included.

Why snake_case field names?

Because that is Python's convention. The original key is used to derive it.

Are numbers int or float?

float, since JSON has one number type. Change to int where the value is always whole.

Does it handle nested objects?

Yes. Each becomes its own dataclass referenced by the parent.

Related tools

JSON to TypeScript

Generate TypeScript interfaces from a JSON sample in your browser. Nested objects become their own interfaces.

JSON to Go Struct

Generate Go structs from a JSON sample in your browser, complete with json struct tags.

JSON to Rust Struct

Generate Rust structs from a JSON sample in your browser, deriving Serialize and Deserialize.