Skip to main content

jsonable_encoder

Convert any object to something that can be encoded in JSON. This function is used internally by FastAPI to ensure anything you return can be encoded as JSON before sending it to the client. You can also use it yourself, for example to convert objects before saving them in a database that supports only JSON.

Function Signature

Parameters

Any
required
The input object to convert to JSON. Can be a Pydantic model, dataclass, dict, list, or any Python object.
set | dict | None
default:"None"
Pydantic’s include parameter, used to specify which fields to include in the output. Can be a set of field names or a nested dict for nested models.
set | dict | None
default:"None"
Pydantic’s exclude parameter, used to specify which fields to exclude from the output. Can be a set of field names or a nested dict for nested models.
bool
default:"True"
If True, use field aliases defined in Pydantic models instead of the Python attribute names. This is useful for APIs where you want to use different names in JSON than in Python code.
bool
default:"False"
If True, exclude fields that were not explicitly set and only have their default values.
bool
default:"False"
If True, exclude fields that have the same value as their default, even if they were explicitly set.
bool
default:"False"
If True, exclude any fields that have a None value from the output.
dict[Any, Callable] | None
default:"None"
A dictionary mapping types to encoder functions. Useful for custom serialization of specific types.
bool
default:"True"
If True, exclude fields starting with _sa from the output. This is a compatibility feature for SQLAlchemy objects, which store internal state in these attributes.

Supported Types

The function handles many Python types automatically:
  • Pydantic models - Converted using model_dump()
  • Dataclasses - Converted to dictionaries
  • Datetime objects - Converted to ISO format strings
  • UUID - Converted to strings
  • Decimal - Converted to int or float
  • Enum - Extracts the .value
  • Path - Converted to strings
  • Sets, frozensets, deque - Converted to lists
  • bytes - Decoded to strings
  • IPv4/IPv6 addresses - Converted to strings

Usage Examples

Basic Pydantic Model

Excluding Unset Fields

Excluding None Values

Including/Excluding Specific Fields

Custom Encoders

With Nested Models

Common Use Cases

Storing in a Database

Patching with Exclude Unset

Preparing for JSON Response

Usage Notes

  • FastAPI uses this internally for response serialization - you usually don’t need to call it explicitly
  • Very useful when working with databases that expect JSON-compatible data
  • Essential for PATCH operations where you only want to update provided fields
  • Handles circular references gracefully for most common cases
  • SQLAlchemy models are supported when sqlalchemy_safe=True
  • Preserves nested structures while converting types recursively

Learn More

Read more in the FastAPI docs for JSON Compatible Encoder.