Convert Python classes to JSON!

Paste Python Pydantic BaseModel, dataclass or TypedDict code and get an example JSON! Field(alias) supported. Handy for API docs and mock request bodies.
Python
Result
 

Convert Python classes to JSON!

Paste Python models written with Pydantic BaseModel, @dataclass or TypedDict, and get back an example JSON document that follows their structure. Field(alias=...) and an alias_generator=to_camel config are read, so you see the keys your snake_case fields really serialize to.

It fits building request examples for FastAPI endpoints, sharing a webhook or queue payload schema with another team, or preparing JSON fixtures for pytest.

How to use

  1. Paste the contents of the .py file holding your models. import lines, methods and decorators such as @field_validator are skipped.
  2. Include referenced sub-models and Enum classes so those parts are filled in. String forward references ("Order") are understood.
  3. On Auto, Root type picks the class that isn't used as a field type by another model; you can also choose one from the list.
  4. Copy the JSON into json_schema_extra examples, a test data file or your API docs.

Example

Input (Python · Pydantic)
class Payer(BaseModel):
    model_config = ConfigDict(alias_generator=to_camel)
    customer_id: str
    email_address: str

class PaymentEvent(BaseModel):
    model_config = ConfigDict(alias_generator=to_camel)
    event_type: Literal["payment.succeeded", "payment.failed"]
    amount: Decimal
    currency: str = "KRW"
    payer: Payer
    receipt_url: Optional[str] = None
    retry_count: int = Field(default=0, alias="retries")
    raw_payload: dict = Field(exclude=True)
Output (JSON)
{
  "eventType": "payment.succeeded",
  "amount": "0",
  "currency": "KRW",
  "payer": {
    "customerId": "",
    "emailAddress": ""
  },
  "receiptUrl": "",
  "retries": 0
}

Because of to_camel in model_config, event_type, receipt_url and customer_id turn into camelCase keys, while the explicit alias="retries" wins for retry_count. raw_payload has Field(exclude=True) and is omitted. The Literal takes its first value, payment.succeeded, and the literal default "KRW" is kept as is.

Supported features

  • Reads Pydantic BaseModel (v1 and v2), @dataclass, TypedDict, and Enum/StrEnum/IntEnum including auto().
  • Key names from Field(alias=...), serialization_alias, and alias_generator (to_camel, to_pascal) set in model_config = ConfigDict(...) or an inner class Config:.
  • Excluded: names starting with _, ClassVar, model_config, and Field(exclude=True) fields.
  • Type handling: Optional[X] / X | None → sample of X, Literal[...] → first value, Annotated/Required/NotRequired → inner type, list/set/tuple → one-element array, dict[str, V] → {"key": V}.
  • Pydantic types: EmailStr, HttpUrl, SecretStr, constr → string; conint, PositiveInt → 0; Decimal and condecimal → the string "0", as Pydantic v2 JSON writes them.
  • Dates and times: datetime → 1970-01-01T00:00:00, date → 1970-01-01, time → 00:00:00, timedelta → PT0S; UUID gets a well-formed value.
  • String, number and True/False literal defaults are used as samples; an enum default such as Status.PAID becomes that member's value.

Use cases

FastAPI request examples

Generate JSON from a request model and drop it into model_config = {"json_schema_extra": {"examples": [...]}}; the example on /docs then uses the same alias keys the endpoint expects.

Sharing webhook and event payloads

If Celery task or Kafka message payloads are defined in Pydantic, give consumer teams on Node or Go the resulting JSON rather than Python code to agree on the structure.

Preparing pytest fixtures

Create the skeleton of a JSON file for model_validate_json() or API client tests, then change individual values to add edge cases.

Notes and limitations

  • Optional fields and fields defaulting to None still get a typed sample instead of null.
  • Decimal is filled as the string "0", matching Pydantic v2 JSON serialization (model_dump_json). If your project emits numbers instead (Pydantic v1, or json.dumps(default=float)), change it to a number.
  • Code that reshapes output, such as @computed_field, @field_serializer or @model_serializer, is not executed.
  • Union[A, B] uses the first non-None member; discriminated unions selected by a discriminator field are not modeled.
  • default_factory and defaults produced by function calls are not run; the value comes from the type.

FAQ

The output uses alias keys, but my model_dump_json() prints field names.

Pydantic serializes by field name unless you pass by_alias=True. FastAPI's response_model uses by_alias=True by default, which matches this tool's output. When dumping yourself, call model_dump_json(by_alias=True).

Dataclasses have no aliases. Can keys still differ from field names?

Standard dataclass has no renaming, so the field name is the key. If a field carries a trailing # JSON: "orderId" comment, as in dataclasses produced by this site's JSON → Python page, that name is used.

Are NotRequired keys of a TypedDict included?

Yes. The sample is meant to show every possible key, so NotRequired and total=False fields are included. Mark the ones that may be omitted in your docs.

Does it handle SQLAlchemy models?

This page is for type-hint based models. For SQLAlchemy models declared with Column(...) or Mapped[...], use the SQLAlchemy → JSON page.