JSON to Python Converter Online
Convert JSON into Python dataclasses, Pydantic models, TypedDicts or NamedTuples, with optional fields, snake_case names and the right imports handled automatically.
How to Convert JSON to Python
Paste a JSON sample, pick a class style, and typed Python classes with the correct imports are generated instantly in your browser.
Krok 1
Vložte svůj JSON
Load an API response or fixture, or start from the built-in example.
Krok 2
Pick a Style
Choose dataclass, Pydantic, TypedDict or NamedTuple, and set the optional syntax.
Krok 3
Copy the Classes
Review the output, then copy it or download it as a .py file.
Turn JSON Into Python Dataclasses, Pydantic Models and TypedDicts
json.loads gives you a dictionary, and a dictionary gives you nothing at the call site. Every field access is a string key that can be misspelled, no editor can autocomplete the shape, and a renamed key fails at runtime rather than at import time. The standard answer is to write classes that describe the payload, and the standard complaint is that writing them by hand from a sample is exactly the kind of boilerplate nobody wants to maintain.
This generator produces typed Python from a JSON sample in four styles. A dataclass suits structured containers with no dependencies. A Pydantic model adds runtime validation, which is what FastAPI request and response models need. A TypedDict adds types to dict-based code without any runtime cost, for mypy and pyright. A NamedTuple gives immutable, unpackable records. Each style comes with its own correct imports, and nested objects become their own named classes rather than collapsing into a dictionary of anything.
The details that make output usable are handled. Nullable fields can be written as Optional[X] or the modern X | None union. Field names are converted to snake_case, which is what Python style expects from camelCase JSON. None of these details are cosmetic: the wrong optional syntax is an error on older interpreters, and a field name that is not valid Python will not import at all. Missing keys can be marked NotRequired in TypedDict output. Optional fields can be ordered last, array item class names are singularised, and from __future__ import annotations is included when the syntax needs it.
Everything runs in your browser. Sample payloads are often the most sensitive JSON you handle, because they come from real endpoints and real customers, and none of it is uploaded here. There is no account, no queue and no limit on how many samples you can run through the generator in a session.
The result is a .py file you can drop into a project. The generator is deterministic, so the same sample always produces the same classes, which matters when the output is committed to a repository and reviewed by a team. Paste the classes in, adjust the fields your application understands better than one sample can, and the typing half of consuming a JSON API is finished before you have opened an editor.
Four Class Styles
Dataclass, Pydantic, TypedDict and NamedTuple output from the same JSON sample.
Optional Field Syntax
Choose Optional[X] or the modern X | None union form for nullable fields.
Nested Classes
Each nested object becomes its own named class with correct type references.
snake_case Fields
camelCase JSON keys are converted to Pythonic field names automatically.
Correct Imports
The right typing, dataclass or pydantic imports are included for each style.
NotRequired Support
Keys missing from the sample can be marked NotRequired in TypedDict output.
Často kladené otázky
Často kladené otázky
Dataclass, Pydantic model, TypedDict and NamedTuple. All four come from the same sample, so you can compare them side by side before choosing the one that fits your project and its conventions.
A dataclass is a typed container with no runtime checking. A Pydantic model validates types when data is loaded, which is what FastAPI and other API frameworks need from their models.
As Optional[X] or as the X | None union, whichever you select. Fields that are null in the sample are the ones that become optional, and the import is added for you.
Yes. camelCase and other JSON key styles become Pythonic snake_case field names, with the original key preserved where the chosen style supports it, and a warning when it cannot be preserved.
Each nested object becomes its own named class, and the parent references it by type, instead of collapsing into a plain dictionary of untyped values that no type checker can follow.
It marks keys that are absent from the sample as genuinely optional, so type checkers do not demand them at every access site in your code, which keeps the types honest.
Dataclasses need Python 3.7 or newer, while TypedDict with NotRequired and the X | None union syntax need newer versions and the matching typing imports before they will run at all in your project.
No. Parsing and code generation run in your browser, so API samples from internal services and staging environments stay on your own machine and are never sent anywhere at any point during generation.
Yes. Every style and option is available without signup, and the generated file carries no watermark or attribution of any kind, so the types are yours to use freely in any project you like.
