About this tool
Working with JSON as plain dictionaries means typos in key names and no editor help. Paste a JSON example and this tool writes typed Python models: Pydantic models that validate data, standard-library dataclasses, or TypedDict definitions that type-check your existing dicts.
Which one should I use?
- Pydantic v2: parses and validates at runtime, converts types, and gives clear errors on bad data. The standard for FastAPI and for any data from outside your program. Keys that aren’t snake_case get a
Field(alias="isActive"), andpopulate_by_name=Truelets you use either name. - dataclass: no dependency, but no validation or nested conversion.
User(**data)leaves nested objects as dicts unless you convert them yourself (or use a library such asdacite). - TypedDict: types for plain dicts, checked by mypy and Pyright but with no runtime cost. Keys are kept exactly, and keys that aren’t valid identifiers use the functional
TypedDict("Name", {...})form.
Types
JSON maps to str, int, float, bool, list[T] and nested classes. Nullable fields use T | None and default to None; optional fields in TypedDicts use NotRequired. The output uses from __future__ import annotations and modern syntax, so it needs Python 3.10 or newer (3.11 for NotRequired). Classes are ordered so each is defined before it is used.
How types are inferred
- Every object becomes its own named type. Names come from the key (
address→Address), and items of an array get the singular (orders→Order). - For arrays of objects, all items are merged into one type: a field missing from some items becomes optional, and a field that is sometimes
nullbecomes nullable. - Integers and decimals in the same field merge to a decimal type. Fields with mixed types (say a number in one item and a string in another) fall back to the language’s “any” type.
- Identical nested shapes are reused instead of generating duplicates.
The code is generated from examples, so it can only be as accurate as the sample. Paste a response that includes optional fields and nulls (or several items) for the best result, then check the types against the API documentation. Generated in your browser: your JSON is not uploaded.
Use the models
# Pydantic v2
user = User.model_validate_json(response.text)
print(user.is_active)
payload = user.model_dump(by_alias=True) # back to the original keys
# TypedDict
data: User = json.loads(text)
FAQ
How do I convert JSON to a Pydantic model?
Paste the JSON above with Pydantic v2 selected. Each object becomes a BaseModel class; keys that are not snake_case get a Field alias so parsing works with the original JSON.
What is the difference between Pydantic and dataclasses?
Pydantic validates and converts data at runtime and builds nested models for you. Dataclasses are plain containers with no validation; nested dicts are not converted automatically.
When should I use TypedDict?
When you want to keep working with plain dicts (for example from json.loads) but get type checking from mypy or Pyright, with no runtime overhead.
Which Python version is needed?
Python 3.10 or newer for the X | None syntax, and 3.11 for NotRequired in TypedDicts (or import it from typing_extensions).
How are keys like "first-name" handled?
They become snake_case attributes (first_name) with an alias for Pydantic, a comment for dataclasses, and are kept as-is in TypedDicts.