Lintify Logo
Lintify
Code Generators

JSON to Python Dataclasses

Generate Python dataclasses (or TypedDicts, or Pydantic models) from JSON. Pick the target library and download a ready-to-import module.

JSON input
1
Python
Output will appear here…

Generate typed Python classes from JSON

Python has three common ways to represent structured data with types: dataclasses (in the standard library since Python 3.7), TypedDict (for typing dict-based code without changing the runtime behavior), and Pydantic (for runtime validation and serialization). Lintify can generate any of the three from a sample JSON document — pick the one that matches your use case.

Dataclasses are the right default for most code. They are in the standard library, work everywhere with no extra dependencies, and integrate cleanly with type checkers. Pydantic is the right pick for API code where you need to validate input at runtime — it gives you validation, serialization, and JSON Schema generation for free. TypedDict is useful when you have existing dict-based code that you want to type without rewriting.

How JSON types map to Python types

JSON strings become str, numbers becomeint or float depending on whether they have a decimal part, booleans becomebool, and null becomesNone (with the field markedOptional[T] in dataclass and Pydantic mode). Arrays become list[T] where T is inferred from the first element. Objects become a new class.

Optional fields with arrays of mixed shapes

When you paste a JSON array, the generator merges the keys of every object. Any key that is missing from at least one element is marked Optional[T] — the field can be None at runtime. This matches what real API responses look like, where some fields are nullable or omitted in certain conditions. For a single object, every field is required because there is no comparison sample.

Class naming and nesting

Each nested JSON object becomes a class named after its parent key, in PascalCase. For example,users[0].address becomes a class namedAddress. The class names are unique within the module, so collisions are resolved by appending a numeric suffix. The output uses Python 3.9+ syntax (list, dict, tuple) rather than the older typing.List imports — add from __future__ import annotations if you need to support older Python.

Frequently asked questions

Common questions about the JSON → Python tool.

Should I pick dataclasses, TypedDict, or Pydantic?
Dataclasses are the standard-library option and work everywhere with no extra dependencies. TypedDict is great for typing existing dict-based code without changing the runtime behavior. Pydantic is the right pick for APIs and validation-heavy code because it gives you runtime validation and serialization for free.
How are JSON types mapped to Python types?
JSON strings become str, numbers become int or float depending on whether they have a decimal part, booleans become bool, null becomes None (and the field becomes Optional[T] in dataclass/Pydantic mode). Arrays become List[T] where T is inferred from the first element. Objects become a new class.
Are optional fields detected?
Yes, when you paste a JSON array. Lintify merges the keys of every object and marks any key that is missing from at least one element as Optional. For a single object, every field is required because there is no comparison sample.
How are nested objects named?
Each nested JSON object becomes a class named after its parent key, in PascalCase. For example, users[0].address becomes a class named Address. The class names are unique within the module, so collisions are resolved by appending a numeric suffix.
Does the generator add type hints?
Yes — type hints are the whole point of the generated code. The output uses Python 3.9+ syntax (list, dict, tuple) rather than the older typing.List imports. If you need to support older Python, you can add from __future__ import annotations at the top of the file.

Related tools