Developer

JSON to Python Dataclass

Generate Python dataclasses with full type hints from a JSON sample, including nested classes, Optional fields, list element types and a from_dict constructor.

Last reviewed by the Radiatus Cloud team

Generated Python appears here.

Need this built for your product?

We design, build & host secure software & APIs.

Talk to an engineer

Typed models beat dictionary access

Reading an API response as a plain dictionary works until the first typo. A key spelled wrong raises at runtime rather than at edit time, no editor can complete a field name, and a change on the provider side is discovered by a KeyError in production. Converting the response into a dataclass moves all of that forward: the fields are declared once, the type checker verifies every access, and the editor knows what exists.

What the generator produces

Each JSON object becomes a dataclass named after the key that held it, in PascalCase, and nested objects become their own classes emitted in dependency order so the file is valid without forward references. Numbers split into int and float, strings that parse as ISO 8601 are annotated datetime with a note, arrays become typed lists, and any key that is missing or null anywhere in the sample becomes Optional with a default of None so construction does not break on a partial payload.

Field names and reserved words

JSON keys are frequently not valid Python identifiers. Keys in camelCase are converted to snake_case, keys with hyphens or spaces are normalised, keys that collide with Python keywords are suffixed, and any key whose original spelling was changed is annotated so you can wire up an alias in whatever deserialiser you use. In Pydantic mode those aliases are emitted directly as Field aliases, so the model round trips against the original JSON without further work.

Related tools

  • JSON Formatter — Format and beautify JSON in your browser. Pinpoints syntax errors by line and column, flags unsafe integers, and never uploads your data to a server.
  • JSON Validator — Validate JSON syntax with precise line and column errors, and check documents against a JSON Schema. Runs locally in your browser, nothing uploaded.
  • Regex Tester — Test regular expressions against sample text with live match highlighting, capture groups and flag control. Runs entirely in your browser.
  • HTML Minifier — Minify HTML by removing comments and redundant whitespace, without breaking inline elements or pre blocks. Runs entirely in your browser.

Frequently Asked Questions

Dataclass or Pydantic, which should I pick?

Dataclasses are standard library and free, but they do not validate: passing a string where an int is annotated is not an error at runtime. Pydantic validates and coerces at construction and handles aliases, at the cost of a dependency. Choose Pydantic for parsing untrusted input.

Why is a field Optional when the JSON always has a value?

It is only Optional when the sample showed a null or the key was missing from at least one record. Paste more records if the sample is small, since a single incomplete record widens the type for everyone.

How are camelCase JSON keys handled?

They are converted to snake_case for the Python attribute, and the original key is recorded. In Pydantic mode a Field alias preserves the mapping; in dataclass mode the generated from_dict reads the original key.

Are datetime strings converted?

Strings matching ISO 8601 are annotated as datetime and the generated from_dict parses them with datetime.fromisoformat. Values that are dates only are annotated date. Anything ambiguous stays a string.

What about a list holding different types?

The element type falls back to Any and a note is emitted. A heterogeneous list usually means the payload is really a union, which is better modelled explicitly than inferred.

Privacy & Security

Everything runs in your browser; nothing is uploaded.

Data: None
Client-side-Side
Active
v1.0

How to Use

Paste JSON, choose dataclass or Pydantic output, and copy the generated Python.

Disclaimer: This tool is provided "as is" without warranty of any kind. Results are for educational and utility purposes.