Skip to main content
The Json response type serializes data to JSON and automatically sets the Content-Type: application/json header.

Basic usage

Return any type that implements serde::Serialize:

How it works

IntoResponse implementation

The Json type implements IntoResponse by serializing the inner value using serde_json:
See implementation in axum/src/json.rs:197-232.

Serialization behavior

The serialization process:
  1. Creates a BytesMut buffer with 128-byte initial capacity
  2. Serializes the value using serde_json::to_writer
  3. On success: Returns response with Content-Type: application/json header
  4. On failure: Returns 500 Internal Server Error with error message as plain text

Error handling

If serialization fails (e.g., map with non-string keys), a 500 status code is returned with the error message:
To prevent status code override on serialization errors, use ForceStatusCode:

Response fields

header
Automatically set to application/json
bytes
The serialized JSON bytes

Common patterns

Returning JSON with custom status

Returning JSON arrays

See also