Need help with your JSON?
Try our JSON Formatter tool to automatically identify and fix syntax errors in your JSON. JSON Formatter tool
Integrating JSON Formatters with Swagger/OpenAPI Documentation
If you want clean, readable JSON in Swagger or OpenAPI docs, the important step is not pasting pretty JSON into markdown. It is attaching valid examples to the correct OpenAPI object so tools like Swagger UI can render and pretty-print them automatically. In most cases, that means putting your sample payload under content.application/json for the request or response you are documenting.
For searchers looking for the practical "swagger json" answer: use example for one payload, use examples for several named payloads, keep schema-level examples for reusable model hints, and run every sample through a JSON formatter before it goes into the spec.
Short version
The most reliable place for formatted JSON in Swagger docs is the media type object: requestBody.content.application/json or responses.[status].content.application/json. Swagger UI renders those examples as formatted JSON and uses them in the interactive docs.
Where Formatted JSON Actually Goes
OpenAPI separates structure from examples. Your schema describes what the JSON should look like. Your request or response content describes concrete payloads that documentation tools can display. That distinction matters because the most visible examples in Swagger UI usually come from the media type level, not just the schema.
- Use
schemato define the shape, required fields, enums, formats, and constraints. - Use
content.application/json.examplewhen you want one sample body. - Use
content.application/json.exampleswhen you want multiple named request or response bodies. - Use schema examples for reusable model-level guidance, not as your only documentation strategy.
A Working Swagger/OpenAPI Pattern
This is the pattern that stays readable in the spec and renders well in Swagger UI: define the model in components/schemas, then attach realistic JSON bodies to the specific operation.
OpenAPI YAML example
openapi: 3.1.1
info:
title: Orders API
version: 1.0.0
paths:
/orders:
post:
summary: Create an order
requestBody:
required: true
content:
application/json:
schema:
$ref: '#/components/schemas/OrderCreate'
examples:
standard:
summary: Standard delivery order
value:
customerId: "cus_123"
currency: "USD"
items:
- sku: "notebook-a5"
quantity: 2
express:
summary: Express order
value:
customerId: "cus_456"
currency: "USD"
shippingMethod: "express"
items:
- sku: "pen-black"
quantity: 10
responses:
'201':
description: Order created
content:
application/json:
schema:
$ref: '#/components/schemas/Order'
example:
id: "ord_789"
status: "created"
total: 42.5
currency: "USD"
components:
schemas:
OrderCreate:
type: object
required: [customerId, currency, items]
properties:
customerId:
type: string
currency:
type: string
shippingMethod:
type: string
enum: [standard, express]
items:
type: array
minItems: 1
items:
type: object
required: [sku, quantity]
properties:
sku:
type: string
quantity:
type: integer
minimum: 1
Order:
type: object
required: [id, status, total, currency]
properties:
id:
type: string
status:
type: string
total:
type: number
currency:
type: stringThis keeps JSON as structured data. Swagger UI can then render it as formatted JSON without guessing.
Choosing Between example, examples, and Schema Examples
These fields look similar, but they solve different problems.
- Media type
example: best when one request or response body is enough. - Media type
examples: best when you want named cases such as success, partial data, validation error, or different business scenarios. - Schema
examples: in OpenAPI 3.1, this is the preferred schema-level way to attach sample values to the model itself. - Schema
example: common in OpenAPI 3.0 documents and still useful when you need compatibility with older tooling.
There is one important rule from the specification: example and examples are mutually exclusive at the same location, and an example defined on the media type overrides an example defined on the referenced schema for that request or response.
Schema-level example in OpenAPI 3.1
components:
schemas:
Address:
type: object
required: [street, city, postalCode]
properties:
street:
type: string
city:
type: string
postalCode:
type: string
examples:
- street: "100 Market St"
city: "San Francisco"
postalCode: "94105"If you still publish OpenAPI 3.0 specs, use singular example on the schema instead.
Reusing JSON Examples Across Endpoints
If the same JSON response appears in several places, move it into components/examples and refer to it with $ref. This keeps large specs easier to maintain and avoids drift between endpoints.
Reusable example object
components:
examples:
OrderCreated:
summary: Minimal order response
value:
id: "ord_789"
status: "created"
total: 42.5
currency: "USD"
paths:
/orders/{id}:
get:
responses:
'200':
description: Order details
content:
application/json:
schema:
$ref: '#/components/schemas/Order'
examples:
default:
$ref: '#/components/examples/OrderCreated' When externalValue Is the Better Option
For huge payloads, event samples, or examples shared across documentation systems, storing raw JSON in a standalone file can be cleaner than embedding it in YAML. In that case, use an Example Object with externalValue.
External JSON example
responses:
'200':
description: Full order export
content:
application/json:
schema:
$ref: '#/components/schemas/Order'
examples:
export:
summary: Large order export example
externalValue: https://docs.example.com/examples/order-export.jsonIn practice, browser-rendered docs only show external examples when that URL is reachable from the docs page.
Common Mistakes That Make Swagger JSON Look Wrong
- Using YAML block strings for object payloads. A pattern like
example: |orexample: |-turns the example into a string. That is only correct if your actual payload is a string, not a JSON object or array. - Declaring both
exampleandexamplesin the same place. Pick one. - Copying invalid JSON into the spec. Trailing commas, comments, single-quoted keys, and mismatched braces break examples fast.
- Letting examples drift away from the schema. If the example does not match the model, docs become misleading and validators may flag the spec.
- Using fake placeholder data everywhere. Samples like
"string"or0do not help developers understand the real contract.
A Practical JSON Formatter Workflow
A JSON formatter is most useful before the example reaches your OpenAPI file. Take the real payload from a response log, contract test, or SDK fixture, then clean it up before you paste it into the spec.
- Format and validate the JSON first so you catch syntax issues immediately.
- Remove unstable values that create noise, such as timestamps or generated IDs, unless they matter.
- Keep values realistic so the sample teaches format, casing, and nesting conventions.
- If your OpenAPI file is YAML, translate the formatted JSON into native YAML objects instead of storing the whole object as a quoted string.
- Add separate named examples for success, validation errors, and edge cases rather than forcing one payload to do everything.
Current Swagger/OpenAPI Notes
Current Swagger guidance still documents the familiar OpenAPI 3.0 example patterns, while the OpenAPI 3.1 specification aligns schema behavior more closely with JSON Schema and prefers schema-level examples over the older singular schema example. The practical takeaway is simple: for the broadest compatibility in generated docs, keep your main request and response payloads on the media type object, and treat schema examples as supporting context.
If you want to confirm exact behavior, check the Swagger documentation on adding examples and the OpenAPI 3.1.1 specification.
Conclusion
The cleanest way to integrate JSON formatting with Swagger/OpenAPI documentation is to pair solid schemas with explicit, validated examples at content.application/json. Format the payload first, keep it as structured data, use named examples when the operation has multiple valid outcomes, and reserve schema examples and externalValue for reuse and scale. That approach produces better Swagger docs and makes the underlying API contract easier to trust.
Need help with your JSON?
Try our JSON Formatter tool to automatically identify and fix syntax errors in your JSON. JSON Formatter tool