CruxDevCruxDev Tools Prompts
dev Utility • Zero-Server Privacy • 100% Client-Side

JSON → Python Dataclass

Generate Python @dataclass or TypedDict definitions from JSON samples with proper type hints and Optional fields.

Overview

Generate a Python dataclass from a JSON object in one step. The converter infers each field's type annotation from the sample values, emits the required dataclass import, and runs entirely client-side, so your payloads never leave the browser.

How It Works

The tool loads with a small demonstration object so the mapping is obvious before you start. Paste your JSON over the sample and the dataclass updates as you type. Invalid JSON or a scalar shows a red error with no code; a top-level array of objects is accepted and described by one dataclass. The output textarea copies its full contents with a single click.

Step-by-Step Usage Guide

  1. Paste the JSON object into the input area.
  2. Read the generated dataclass in the output panel and tighten any Any annotations.
  3. Check fields the tool renamed because their JSON key was not a valid Python identifier, and map them back to the original keys in your loader (the # JSON key comment shows each one).
  4. Click the output to copy the class into your module.

Technical Specifications & Standards

Inference maps JSON strings to str, booleans to bool, whole numbers to int, and decimals to float. A field that is always null becomes Any, arrays become list[T] with the element types of all items merged, an empty array becomes list[Any], and each nested object becomes its own dataclass defined before the class that uses it. Required fields come first, and fields that can be null or are missing from some elements are written as T | None = None. Field names are kept when they are valid Python identifiers, so camelCase keys stay camelCase; keys that are illegal identifiers or Python keywords are renamed to snake_case (keywords get a trailing underscore) with a # JSON key comment showing the original. Dataclasses give you __init__, __repr__, and __eq__ automatically, which is usually what you want for API models. For runtime validation consider layering pydantic or attrs on top: the generated annotations are a correct starting point for both. One caveat to remember: the browser's Number type stores all JSON numbers as doubles, so an integer larger than 2^53 is labelled float rather than int.

Targeted Use Cases

  • Typing API response objects before writing a client wrapper.
  • Creating dataclasses for configuration stored as JSON.
  • Preparing test fixtures with realistic typed models.
  • Teaching material that shows JSON types next to Python type annotations.

Notes & Gotchas

  • Give every array at least one element so its item type is inferred rather than left as list[Any].
  • Replace Any with a specific type once you know the real type of nullable fields; nullable fields are already written as T | None = None (Python 3.10+ syntax).
  • Convert camelCase JSON keys to snake_case fields and translate them back in a from_dict method or pydantic alias.
  • Validate parsed data at runtime; inferred annotations are not enforced by default.

Frequently Asked Questions

Why int for some numbers and float for others?

The generator looks at the sample value: a JSON number written without a decimal point is treated as an integer, anything with a fractional part becomes float. If your API can return both for one field, pick float or a union type yourself.

Does it nest dataclasses for nested objects?

Yes. Each nested object becomes its own dataclass, defined before the class that uses it, and the field is annotated with that class.

Is my JSON uploaded anywhere?

No. The conversion runs in your browser with standard JavaScript parsing. Once the page has loaded it works with the network disconnected.

Why not generate pydantic models directly?

The tool targets the standard library's dataclasses so the output has no dependencies. The field annotations transfer to pydantic BaseModel fields with minimal edits if you prefer validation.