Generate Python Models from JSON Samples
Use this JSON to Python converter to create a first draft of Python types from an example API response, configuration file, webhook payload, or test fixture. Choose TypedDict when your code works with dictionaries and you want a type checker to understand their keys. Choose a dataclass when you want named instances with attributes. The generated code is a model of the examples you provide, not a complete description of every value your application may receive.
Paste JSON or open a local file, set a root type name, select a model style, and generate the code. The tool infers nested object models, list item types, nullable values, and keys missing from some sample objects. You can include sample data, then copy the result or download a .py file. Conversion happens in your browser; the JSON is not sent to a conversion service.
How to Convert JSON to Python
- Paste a valid JSON object or array into the input, or select Open JSON.
- Enter a valid Python class name, such as
Person. - Choose TypedDict or Dataclass.
- For TypedDict output, leave Mark missing fields as optional on if some records omit keys. This control is not used for dataclass output.
- Choose whether to include a sample dictionary or instance, then select Generate Python.
- Review the inferred fields and types before copying or downloading the code.
When the input is an array of objects, include a few representative records, especially records that show optional keys, null values, and different nested shapes.
Optional Keys and Nullable Values Are Different
In a TypedDict, NotRequired[T] means a dictionary may omit that key. Optional[T] means the key’s value may be None. A key can be both optional and nullable, which is useful for API fields that sometimes do not appear and may also explicitly contain JSON null.
Sample JSON
[
{ "id": 101, "name": "Ada", "nickname": null },
{ "id": 202, "name": "Grace", "nickname": "Amazing Grace" },
{ "id": 303, "name": "Katherine" }
]
With the root name set to Person, the TypedDict output includes:
class Person(TypedDict):
id: int
name: str
nickname: NotRequired[Optional[str]]
NotRequired reflects that the third record has no nickname key; Optional[str] reflects the explicit null in the first record. Dataclass output handles a missing field with a default of None, but that does not distinguish an omitted key from one that was explicitly null.
TypedDict or Dataclass?
| Output style | Use it when | What the generated code provides |
|---|---|---|
TypedDict | Your data is naturally a dictionary, such as decoded JSON. | Key and value annotations for static type checkers; optional keys use NotRequired when enabled. |
Dataclass | You want instances with named attributes and a generated initializer. | Nested dataclasses, field annotations, and None defaults for missing sample fields. |
Both styles use Python type annotations. TypedDict instances are ordinary dictionaries at runtime, and their declared key types are not enforced automatically. Dataclasses provide a convenient object shape, but they do not parse JSON dictionaries into instances by themselves.
How JSON Values Map to Python Types
| JSON value | Typical Python annotation | What to review |
|---|---|---|
"Ada" | str | Date-like strings remain strings. |
101 | int | Large JSON integers may lose precision during browser parsing. |
12.5 | float | Check precision if exact decimal values matter. |
true | bool | JSON booleans map to Python booleans. |
null | Optional[T] or Any | A null-only field has no sample type to infer. |
["read", "write"] | list[str] | Empty arrays provide no example of the item type. |
{ "city": "London" } | A nested model | Nested object types are derived from their property names. |
When sample values for a field have incompatible types, the generator may use Any instead of guessing a union. An empty list is typed as list[Any] because it contains no element example. Add representative values or refine those annotations to match the data contract.
How Names and Nested Objects Are Handled
Nested JSON objects become separate named TypedDicts or dataclasses, using class-style names derived from their property names. Dataclass field names are converted to lowercase with underscores where appropriate. TypedDict output preserves the original JSON key; if that key is not a valid Python identifier, the generator uses TypedDict’s functional form so the key can still be represented.
Dataclasses use Python attribute names, so a converted name such as first_name does not automatically map to an external JSON key such as first-name. If you need to serialize or deserialize such data, configure a JSON library or add explicit mapping code.
Python Version Notes
The generated list annotations use built-in generics such as list[str], which require Python 3.9 or newer. When optional TypedDict keys are inferred, the output imports NotRequired from typing, which is available in Python 3.11 and later. For older Python versions, use a compatible typing_extensions import or adapt the generated declaration. Confirm the target Python version for your project before checking the code in.
Generated Types Do Not Validate JSON at Runtime
The generator parses the sample and writes type declarations and optional example data. It does not validate future input, enforce business rules, or turn a JSON dictionary into a dataclass instance. Use a runtime validation or serialization library when data from an API or user must be checked before your program relies on it. If you need a machine-readable JSON contract, start with the JSON Schema Generator and review its inferred requirements.
Frequently Asked Questions
Does TypedDict validate a dictionary when Python runs?
No. TypedDict describes the expected keys and value types for static type checkers. At runtime it is still a regular dictionary, so validate untrusted data separately.
What is the difference between NotRequired and Optional?
NotRequired[T] means a TypedDict key may be absent. Optional[T] means its value may be None. They describe separate cases and can be combined.
Can I generate dataclasses from a JSON array?
Yes. Arrays of objects are merged into a shared model shape, and dataclass sample output constructs an instance for each supplied object.
Why does an empty array use Any?
There are no array items to inspect, so the converter cannot infer an element type. It uses list[Any] until you provide a representative value or refine the annotation.
Which Python version supports the generated code?
The output uses built-in generic annotations such as list[str], so use Python 3.9 or newer. Optional TypedDict keys use typing.NotRequired, which requires Python 3.11 or newer unless you adapt the import to typing_extensions.
Does the converter upload my JSON?
No. Conversion takes place in your browser. For sensitive content, follow your organization’s data-handling rules and review the privacy information.
Related JSON Tools
Check your sample with the JSON Validator, make it easier to inspect with the JSON Formatter, or explore nested values in the JSON Viewer. You can also generate other model formats with the JSON to Java Converter and JSON to TypeScript Converter.