JSON to Python

The JSON to Python Generator converts a JSON sample into Python dataclass or TypedDict definitions with accurate type annotations, nested class hierarchies, and Optional field markers. Use it to add static type checking to REST API response handling, FastAPI models, and data pipeline code without writing class definitions by hand.

S. Siddiqui

Edited by

S. SiddiquiFounder & Editor-in-Chief
Sources:MDN Web DocsW3CIETFUpdated Jun 2026

What Is the JSON to Python Generator?

The JSON to Python Generator analyses a JSON sample and produces Python code that represents the same data structure. The tool supports two output modes: Python dataclasses (using the @dataclass decorator introduced in Python 3.7) and TypedDict (a typed dictionary available from Python 3.8). Both modes give you a typed, self-documenting representation of your JSON data that enables IDE autocomplete, static type checking with mypy or Pyright, and cleaner, more maintainable code compared to working with raw dictionaries.

Python's built-in json.loads() function returns a plain dict — an untyped structure where every key access returns Any. This is convenient for small scripts but becomes a maintenance problem in larger codebases: typos in key names only surface at runtime, field types are undocumented, and refactoring is error-prone because no static tool can verify field access. Generating a dataclass or TypedDict from a JSON sample gives you the same type safety that TypeScript interfaces provide for JavaScript developers.

According to the Python 3 dataclasses documentation, the @dataclass decorator generates __init__, __repr__, and __eq__ methods automatically, making dataclasses ideal for structured data objects. Quora discussions confirm that generating Python dataclasses from JSON automatically is a common developer need — particularly for developers working with REST APIs, data science pipelines, and Django or FastAPI applications where typed request and response bodies are expected.

This generator is used by Python developers integrating REST APIs, data scientists loading JSON datasets into typed structures, FastAPI developers who need Python models for API endpoint validation, and Django developers working with serialised data from external services.

How to Use the JSON to Python Generator

  1. Paste your JSON sample. The input should be a JSON object or array representative of the data you will work with in Python. Include examples of all field types — strings, numbers, booleans, null values, nested objects, and arrays — so the generator can produce accurate type annotations.
  2. Choose your output mode. Select Dataclass for a class-based representation with an auto-generated constructor and methods — ideal for data manipulation, serialisation, and object-oriented code. Select TypedDict for a dict-compatible typed structure — ideal when you want to keep working with plain dictionaries but gain type checking without changing how you access fields.
  3. Choose a class name. Enter a PascalCase name for the root class — for example User, Product, or ApiResponse. Nested objects generate additional classes named from the property name.
  4. Review the output. Check type annotations for accuracy. Fields with JSON null values produce Optional[Type]. Fields present in some objects but not others should be marked Optional[Type] = None. Arrays of objects produce List[ClassName].
  5. Copy and use in your project. Paste the generated code into your Python file. For dataclasses, import dataclass from the dataclasses module and Optional, List from typing. To populate a dataclass from a JSON dict, use a library like dacite or write a from_dict class method.

Why Use This Tool

Writing Python dataclass definitions by hand from a JSON sample is repetitive. A JSON object with 15 fields requires 15 type-annotated attributes, each correctly mapped from its JSON type to the appropriate Python type. For nested structures, this cascades into multiple classes. This tool produces all of that code in seconds from a real sample, leaving you to make only the semantic decisions — which fields are optional, which need additional validation.

FastAPI developers defining request and response models

FastAPI uses Pydantic models for request body validation and response serialisation. While the generated dataclass code is not directly a Pydantic model, it provides the field names and types needed to write one quickly. Many FastAPI developers use this tool to generate a dataclass first, verify the field structure, and then convert it to a Pydantic BaseModel by changing the class declaration — a one-minute task that saves 15 minutes of manual transcription.

Data scientists loading JSON datasets into typed structures

Data science projects frequently load configuration files, API responses, or experiment result logs in JSON format. Working with raw dictionaries makes data access fragile and undocumented. Generating a dataclass from a representative sample gives the dataset a typed schema: IDE autocomplete works on field names, mypy catches field access errors, and the class definition serves as living documentation of the dataset's structure.

Django REST Framework developers

Django REST Framework (DRF) serialisers define how JSON request bodies are validated and how model instances are serialised to JSON. When integrating with an external API, a Django developer often needs to understand the exact JSON structure before writing a serialiser. This tool provides a typed Python representation of the external API's JSON format, which serves as a reference when writing the DRF serialiser class.

Developers replacing raw dict access with typed code

In any Python codebase that processes JSON, the transition from data["user"]["email"] to typed attribute access — data.user.email with autocomplete and type checking — requires defining a dataclass or TypedDict. This tool generates those definitions from existing JSON samples, allowing teams to incrementally type their JSON handling code without rewriting it.

Real-World Use Cases

FastAPI developer typing a third-party API integration

A developer builds a FastAPI microservice that calls the OpenWeatherMap API and returns formatted weather data to a mobile app. The OpenWeatherMap response is a complex nested JSON with dozens of fields. Rather than writing the Python dataclass by hand, the developer makes one API call, captures the response, and pastes it into this generator. The resulting dataclass hierarchy — WeatherResponse, WeatherMain, Wind, Clouds — is pasted into a models.py file. The developer then selects only the fields needed and converts the relevant classes to Pydantic models for the FastAPI response schema.

Data scientist typing a machine learning experiment configuration

A data scientist runs experiments using a JSON configuration file that specifies model hyperparameters, dataset paths, and training settings. To avoid typos in configuration key access (config["learnign_rate"] vs config["learning_rate"]), the scientist generates a TrainingConfig dataclass from a representative configuration JSON. All configuration access in the training script goes through the typed dataclass, and mypy catches any misspelled field names at check time rather than mid-training run.

Python developer integrating a Stripe API response

A back-end Python developer handles Stripe payment intent webhook payloads in a Django view. The payload is a deeply nested JSON object. Using this generator, the developer produces Python dataclasses for the relevant portions of the Stripe webhook structure. The dataclasses are used as type hints in the webhook handler function, making the code self-documenting and enabling Pyright to verify that all field accesses are valid without running the application.

Team adopting type checking in a legacy codebase

A team introduces mypy to a legacy Django codebase and begins adding type annotations. Their API integration layer processes JSON from several external services using raw dictionary access. Using this generator for each service's JSON format, they produce TypedDict definitions that can be applied incrementally — wrapping existing dict access with TypedDict casts without changing the underlying data structures. Mypy immediately flags 12 incorrect field access patterns across the codebase that had been silently returning None in production.

Common Mistakes and Troubleshooting

Not handling Optional fields correctly

Fields that are null in the JSON sample are typed as Optional[type] (equivalent to Union[type, None]). Fields that are sometimes absent from real responses should also be Optional with a default of None: email: Optional[str] = None. If you use a dataclass populated from a JSON dict, accessing a field that is None as if it were a non-None value will raise an AttributeError at runtime. Always check for None before accessing optional fields.

Confusing dataclass instantiation with JSON parsing

A generated dataclass defines the structure but does not automatically parse a JSON dict into an instance. MyClass(**json.loads(response)) only works for flat objects with matching key names. For nested structures, use the dacite library: pip install dacite, then from dacite import from_dict; obj = from_dict(data_class=MyClass, data=json.loads(response)). Dacite recursively maps nested dicts to nested dataclasses, handling type coercion and optional fields.

Python version compatibility

The @dataclass decorator requires Python 3.7 or later. TypedDict requires Python 3.8 or later for the class-based syntax. The from __future__ import annotations import (for deferred annotation evaluation) is recommended for Python 3.7–3.9 but is the default in Python 3.10+. If your project targets Python 3.6, use attrs or NamedTuple as alternatives to dataclasses.

JSON keys that are not valid Python identifiers

JSON keys containing hyphens (created-at), spaces, or keywords reserved by Python cannot be used directly as dataclass field names. The generator converts these to snake_case equivalents (created_at). If you need to deserialise from JSON keys with hyphens, use Pydantic (which supports Field(alias="created-at")) or write a custom from_dict class method that remaps keys during instantiation.

Large integer values typed as int but received as float from the API

Python's json.loads() produces int for JSON integers and float for JSON floats. The generator reflects this. However, some APIs return large IDs as floating-point numbers (e.g. 12345678901234.0), which json.loads parses as float in Python. If you see unexpected float types for ID fields, check the raw JSON to confirm the value has no decimal point — if it does not, the type annotation should be int and the value will parse correctly.

Last reviewed: June 7, 2026

Frequently Asked Questions

What is a Python dataclass and why use it for JSON data?
A Python dataclass (decorated with @dataclass from the dataclasses module, available since Python 3.7) is a class that automatically generates __init__, __repr__, and __eq__ methods from its type-annotated attributes. For JSON data, a dataclass provides typed field access (data.user.email instead of data['user']['email']), IDE autocomplete, mypy/Pyright static type checking, and self-documenting code. It is the Python equivalent of a TypeScript interface for API response typing.
What is the difference between @dataclass and TypedDict for JSON?
Dataclasses create proper Python class instances with attribute access syntax (obj.name) and support methods, default values, and inheritance. TypedDict creates a dict subtype that maintains dict access syntax (obj['name']) but adds static type checking. Use dataclasses when you want object-oriented access, methods, and constructor-based instantiation. Use TypedDict when you need to pass the data to functions that expect plain dicts, or when retrofitting types onto code that already uses dict access patterns.
How do I deserialise JSON into the generated dataclass?
For flat dataclasses: from_instance = MyClass(**json.loads(json_string)) works if the JSON keys match exactly. For nested dataclasses, use the dacite library: pip install dacite, then from dacite import from_dict; obj = from_dict(data_class=MyClass, data=json.loads(json_string)). Dacite recursively converts nested dicts into nested dataclass instances. For Pydantic models (a popular alternative), use MyModel.model_validate(json.loads(json_string)) which handles nesting automatically.
How are nested JSON objects handled in the output?
Each nested JSON object produces a separate Python dataclass. The class name is derived from the property name converted to PascalCase — an address property produces an Address dataclass, and userProfile produces a UserProfile dataclass. The parent dataclass references the child class as a type: address: Address. All generated classes are listed in the output and must all be included in your Python file for the type annotations to resolve correctly.
How are optional fields represented in Python dataclasses?
Fields that are null in the JSON sample are typed as Optional[type] (for example, Optional[str]). In Python 3.10+, you can use the more readable type | None syntax. For fields that should be optional with a default value, the generator adds = None: email: Optional[str] = None. Fields with defaults must come after fields without defaults in dataclass definitions — the generator handles this ordering automatically.
What Python version is required for the generated code?
The @dataclass decorator requires Python 3.7+. TypedDict requires Python 3.8+. The Optional and List type hints from the typing module are available from Python 3.5. In Python 3.9+, you can use list[str] and dict[str, Any] directly instead of List[str] and Dict[str, Any] from typing. In Python 3.10+, use str | None instead of Optional[str]. The generator targets Python 3.8+ compatible syntax by default.
Can I use the generated dataclass with Pydantic?
Not directly — a @dataclass is not a Pydantic BaseModel. However, the generated dataclass gives you the field names and types you need to write a Pydantic model quickly: replace @dataclass with class MyModel(BaseModel) and the field definitions are identical. Pydantic v2 also has a @dataclasses.dataclass compatibility layer via pydantic.dataclasses.dataclass. For FastAPI specifically, use Pydantic BaseModel rather than plain dataclasses, as FastAPI relies on Pydantic for request validation and OpenAPI schema generation.
How does the generator handle JSON arrays?
JSON arrays of objects produce List[ClassName] (or list[ClassName] in Python 3.9+). JSON arrays of strings produce List[str]. JSON arrays of mixed types produce List[Union[str, int, None]] or similar. Root-level JSON arrays — where the top-level value is an array rather than an object — produce a type alias: UserList = List[User]. Import List from typing in Python 3.8 and earlier; in 3.9+ the built-in list is generic and List from typing is no longer needed.
What is the dacite library and how does it help?
dacite (pip install dacite) is a Python library that converts plain dictionaries into dataclass instances recursively. Without dacite, nested dict-to-dataclass conversion requires manual nesting: User(address=Address(**data['address']), **{k:v for k,v in data.items() if k != 'address'}). With dacite: from_dict(data_class=User, data=response_dict). Dacite handles nested classes, optional fields, type coercion, and forward references automatically, making it the standard library for populating complex dataclass hierarchies from parsed JSON.
How are JSON keys with hyphens handled in Python?
JSON keys containing hyphens (like created-at or X-Request-ID) are not valid Python identifiers and cannot be used as attribute names. The generator converts them to snake_case: created-at becomes created_at and x-request-id becomes x_request_id. To deserialise JSON with hyphenated keys into these renamed fields, use dacite with a remapping: from_dict(data_class=MyClass, data=remap_keys(raw_dict)) where remap_keys replaces hyphens with underscores. Pydantic handles this with Field(alias='created-at') on the model field.

Rate This Tool

Was this tool helpful?

Be the first to rate this tool

About the Author

S. Siddiqui

S. Siddiqui

Founder & Editor-in-Chief

LinkedIn Profile

S. Siddiqui is the founder and editor-in-chief of YourToolsBase, overseeing all content, tool accuracy, and editorial standards.

View full profile

Authoritative Sources

Formulas and data in this tool are based on guidelines from the above sources.