AIWalay Tools

JSON to Python Dataclass Converter

Generate Python dataclasses from sample JSON. Infers types, converts camelCase to snake_case, marks optionals. Modern or typing-module style. Free.

About the JSON to Python Dataclass Converter

Paste sample JSON and this tool generates matching Python dataclasses with type hints. It infers each field's type — str, int, float, bool, None, nested classes and lists — and turns nested objects into their own dataclasses, giving you clean, typed models to drop into a Python project.

It handles the details that make generated Python actually usable. camelCase and kebab-case JSON keys are converted to snake_case field names (and Python keywords get a trailing underscore), fields that are missing in some array items become Optional with a None default, and mixed-type fields become proper unions. You choose between modern Python 3.10+ syntax (str | None, list[int]) and classic typing-module style (Optional[str], List[int]), and the correct imports are added automatically.

Set the root class name, generate, then copy or download a ready-to-use .py file. Everything runs in your browser, so private payloads never leave your machine.

How to Use the JSON to Python Dataclass Converter

  1. 1Paste a representative JSON sample into the input box.
  2. 2Set the root class name and choose the type-hint style.
  3. 3Click Generate dataclasses.
  4. 4Copy the code or download it as a .py file.

Frequently Asked Questions

Does it convert JSON keys to Python naming conventions?

Yes. camelCase and kebab-case keys become snake_case field names to match PEP 8, and any key that collides with a Python keyword (like class or from) gets a trailing underscore. Keys starting with a digit are prefixed so they're valid identifiers.

What's the difference between the two type-hint styles?

Modern style uses built-in generics and the union operator introduced in Python 3.10+, so you get str | None and list[int] with no imports. The typing-module style uses Optional[str] and List[int] from typing, which works on older Python versions. Pick whichever matches your target runtime.

How are optional fields decided?

When the JSON root is an array of objects, any key that's absent from some items is marked optional and given a default of None, so instantiating the dataclass doesn't require every field. Null values also produce Optional types.

How does it avoid class definition-order problems?

Nested class references are emitted as forward-reference strings, so the dataclasses work regardless of the order they appear in the file — you won't hit NameError from referencing a class defined later.

Is my JSON sent to a server?

No. Code generation runs entirely in your browser, so you can safely paste real API payloads with sensitive field names or values.

Related Tools