Type generation

JSON to Pythondataclass and Pydantic models

Compare both sample records. Missing keys and explicit null produce different field defaults.

JSON to Python

JSON input

Parsed and generated in this browser. Input is not uploaded or stored.

Generated source

Generation options

Python 3.9+. dataclass metadata does not decode or validate. Pydantic 2.12.5 includes load_models(source); use it for the selected number policy.

Array samples define the element model Root. Pydantic load_models accepts either an object or an array.

2 classes · 2 nullable fields

JSON to Python turns representative API responses into a model draft. Include records with different keys: a single complete object cannot show which fields may be absent.

Choose standard-library dataclasses for typed containers or JSON to Pydantic for runtime parsing. Samples provide evidence about types, not a complete contract or business validation rules.

Why use JSON to Python?

Separate missing from null

JSON to Python keeps an always-present nullable field required. A key absent from another sample instead receives a None default. These are different constructor and validation contracts.

Two object cards show a hollow null marker and an absent slot connecting to different model field defaults.

Keep original keys connected

Reserved words, punctuation and model methods need safe Python names. The generator retains the original spelling in dataclass metadata or Pydantic aliases, with deterministic suffixes for collisions.

Irregular key shapes connect to safe field slots through separate metadata and validated alias paths.

Choose a parsing contract

01

Supply contrasting records

Use one object or a nonempty array containing only objects. All objects at a nested array location contribute to one element model; missing keys are discovered across those samples.

02

Review defaults before importing

Missing-key mode adds None only when a key is absent; all-optional mode permits omission everywhere. Date strings stay str unless ISO inference is selected. Decimal affects decimal or exponent tokens and numeric groups containing them.

03

Export a fresh result

Generation waits 350 ms after editing. Disable automatic mode for the Generate button or Ctrl/Cmd+Enter. Changes disable exports immediately. Clear and Example keep one undo snapshot of input, settings and source.

Types and generated behavior

Two frameworks, one source file

JSON to Python dataclass output imports only the standard library and places required fields before defaulted fields. Pydantic output targets exactly version 2.12.5. Child classes are declared before their parents, so Python 3.9 can import the entire file.

Numbers with an explicit policy

Plain integer groups use arbitrary-precision int. Decimal or exponent groups use float by default, including integers merged into that group; ordinary binary rounding applies. Decimal mode uses Decimal for those groups and its loader parses decimal tokens without a float intermediate, even inside Any containers.

Conservative date inference

ISO mode recognizes valid calendar dates and full timestamps with a timezone and at most six fractional second digits. All non-null strings at a position must have the same date kind. Invalid dates, naive timestamps and more precise strings stay str. Pydantic converts recognized strings; dataclasses do not.

Safe identifiers and aliases

Fields become ASCII snake_case. Keywords, leading underscores, model_* names and BaseModel methods are renamed. Empty keys, Unicode and control characters retain their aliases. Imported modules use private aliases to avoid collisions with class names such as Decimal or BaseModel.

What the generated file promises

Dataclasses are containers
Python type hints and json_key metadata do not validate input, rename incoming keys, construct nested objects or parse dates. Build dataclass instances explicitly. Passing a nested dict to a constructor leaves it a dict; asdict uses Python attribute names, not the metadata aliases.
Use the Pydantic parsing helper
Install pydantic==2.12.5 and call load_models(source). The helper applies json.loads with the selected float/Decimal policy before model_validate, and returns a model or list. Calling model_validate_json directly does not carry the helper’s Decimal guarantee. Decimal serialization normally produces JSON strings.
Validation has deliberate boundaries
Pydantic accepts original JSON aliases only and rejects unknown properties with extra="forbid". Optional[T] permits null; only a default permits omission. Missing fields default to None, so the value alone does not preserve absent versus null; model_fields_set can distinguish them. Inferred types use normal Pydantic coercion, not strict business validation.
Uncertain samples stay broad
Conflicting JSON kinds and null-only fields use Any, empty arrays use List[Any], and empty nested objects use Dict[str, Any]. No enums, UUID, EmailStr, TypedDict, JSON Schema, OpenAPI or ORM generation is included. Root scalars, empty arrays and mixed root arrays are rejected.
Mappings and numeric range can fail
Disabling aliases rejects every key that would need renaming. Unpaired Unicode surrogate keys are rejected in both frameworks. Float mode rejects overflow and nonzero decimal tokens that underflow to zero; it does not promise decimal accuracy. Choose Decimal for exact financial or scientific decimal values.
Bounded, strict input
No comments, trailing commas, duplicate keys, automatic repair, file import, URL fetching or Python execution. Limits are 500,000 input characters, 50,000 nodes, depth 40, 100 fields per merged object, 200 classes, 1,000 characters per key/number token, exponent magnitude 10,000 and 1,000,000 output characters.

JSON to Python questions

When should I choose dataclass rather than Pydantic?

Choose dataclass for a standard-library typed container when your code already handles parsing. JSON to Python also offers Pydantic when you need nested object construction and validation. Use its generated load_models helper and review the coercion and unknown-field rules.

Which Python release can run the generated models?

The generated syntax targets Python 3.9 and later. It uses typing.Optional and orders dataclass defaults after required fields. Pydantic output is tested against 2.12.5; it is a fixed compatibility target, not a claim to use the latest release.

Can nested JSON objects get their own classes?

Yes. Nonempty nested objects create helper classes in the same file, with child classes first. Arrays merge all their elements. An array at the root defines the element class, and the Pydantic helper returns a list of those instances.

Can I construct a dataclass from JSON without dependencies?

Yes, with your own explicit conversion code, but these generated dataclasses do not include that decoder. json.loads returns dictionaries and lists. You must map aliases and construct nested dataclasses yourself; metadata alone does not perform either step.

How can camelCase keys map to snake_case attributes?

Keep aliases enabled. Pydantic Field(alias=...) reads the original JSON key; dataclasses store it as json_key metadata for your own adapter. Collision suffixes distinguish keys such as a-b and a_b. The Pydantic configuration avoids falling back to another field’s Python name.

Found an inference case to improve?

Review your model with real records

Use JSON to Python with missing keys, nulls and representative numbers, then test the exported source against your application’s parsing contract.