YAML is a superset of JSON. This means you can use JSON syntax for objects and arrays within a YAML document.

In your first example, you can just remove the | after the value: to keep the example value as an object.

The following are equivalent:

examples:
  singlePet:
    summary: Single pet
    description: A request containing a single pet
    value:
      {
        "pets" : [
          {
            "petType" : "DOG",
            "name" : "Ben"
          }
        ]
      }
examples:
  singlePet:
    summary: Single pet
    description: A request containing a single pet
    value:
      pets:
      - petType: DOG
        name: Ben

Alternatively, you can use externalValue to point to an external file containing sample JSON.

examples:
  singlePet:
    summary: Single pet
    description: A request containing a single pet
    externalValue: 'https://api.example.com/docs/examples/pet.json'
Answer from Helen on Stack Overflow
🌐
OpenAPI
learn.openapis.org › specification › structure.html
Structure of an OpenAPI Description
Here’s an example of a minimal OpenAPI Description: openapi: 3.1.0 info: title: A minimal OpenAPI Description version: 0.0.1 paths: {} # No endpoints defined · This API is not very useful because it defines no operations (it has no endpoints). The next page remedies that. ... The syntax (language) used to write OpenAPI Descriptions can be JSON, YAML or both.
🌐
I'd Rather Be Writing
idratherbewriting.com › learnapidoc › pubapis_yaml.html
Working in YAML (OpenAPI tutorial) | I'd Rather Be Writing Blog and API doc course
2 weeks ago - For the most part, YAML and JSON are different ways of structuring the same data. Dot notation accesses the values the same way. For example, the Swagger UI can read the openapi.json or openapi.yaml files equivalently. Pretty much any parser that reads JSON will also read YAML.
🌐
Redocly
redocly.com › learn › openapi › learning-openapi
Introduction to OpenAPI
You can use `markdown` here. ... application/json: schema: $ref: ../components/schemas/User.yaml example: username: user1 email: user@example.com '403': description: Forbidden '404': description: User not found put: tags: ...
🌐
Microsoft Learn
learn.microsoft.com › en-us › openapi › openapi.net › convert-openapi
Convert an OpenAPI document | Microsoft Learn
May 24, 2024 - Compare the original YAML with the new JSON. openapi: 3.0.1 info: title: PetStore API version: 1.0.0 servers: - url: https://api.petstore.com paths: /pets: get: description: Get all pets responses: '200': description: A list of pets content: application/json: schema: type: array items: $ref: '#/components/schemas/Pet' /pets/post: post: description: Add a new pet requestBody: content: application/json: schema: $ref: '#/components/schemas/Pet' required: true responses: '201': description: Pet created successfully components: schemas: Pet: type: object properties: name: type: string category: type: object properties: id: type: integer name: type: string
🌐
Swagger
swagger.io › docs › specification › v3_0 › basic-structure
Basic Structure | Swagger Docs
You can write OpenAPI definitions in YAML or JSON. In this guide, we use only YAML examples but JSON works equally well.
🌐
Swagger
swagger.io › specification
OpenAPI Specification - Version 3.1.0 | Swagger
The OpenAPI Specification defines a standard interface to RESTful APIs which allows both humans and computers to understand service capabilities without access to source code, documentation, or network traffic inspection.
Find elsewhere
🌐
Atlassian
lf-onap.atlassian.net › wiki › display › DW › Adding+examples+for+Request+and+Response+in+openapi.yaml
Adding examples for Request and Response in openapi.yaml - ONAP Wiki - Confluence
You can add examples to parameters, properties and objects to make OpenAPI specification of your web service clearer. Examples can be read by tools and libraries that process your API in some way. To specify an example, you use the example or examples keys. ... requestBody: required: true content: ...
🌐
OpenAPI
spec.openapis.org › oas › v3.1.0
OpenAPI Specification v3.1.0 | Introduction, Definitions, & ...
February 16, 2021 - An OpenAPI document MAY be made ... and Schema Object $ref keywords are used. It is RECOMMENDED that the root OpenAPI document be named: openapi.json or openapi.yaml....
🌐
OpenAPI
learn.openapis.org › specification › docs.html
Providing Documentation and Examples
paths: /audio/volume: put: requestBody: required: true content: application/json: schema: type: integer minimum: 0 maximum: 11 description: Current volume for all audio output. 0 means no audio output (mute).
🌐
Openapispec
openapispec.com › docs › how › how-does-openapi-support-json-and-yaml-formats
How does OpenAPI support JSON and YAML formats? | OpenAPI Specification
One of the major advantages of YAML over JSON is that it tends to be more readable due to its use of indentation rather than braces and brackets. ... openapi: 3.0.0 info: title: Sample API description: This is a sample API version: 1.0.0 paths: /example: get: summary: Example endpoint responses: '200': description: Successful response
🌐
OpenAPI Converter
openapi-converter.com › json-to-yaml
OpenAPI JSON to YAML Converter - Free Online Tool
Free online OpenAPI/Swagger JSON to YAML converter. Paste JSON or open a .json file and get pretty-printed YAML from OpenAPI 3.0, 3.1 and Swagger 2.0 specifications.
🌐
Stoplight
stoplight.io › openapi
OpenAPI | OpenAPI Definition & Online Tools | Open API Standards List | Stoplight
In this snippet of OpenAPI YAML, you can see the data types and example data are stored alongside each other: get: summary: customer tags: [] responses: '200': description: OK content: application/json: schema: type: object properties: customer_id: type: integer example: 123456 customer_name: type: string example: Roy Fielding operationId: get-customers-customer_id description: Retrieve a specific customer by ID
🌐
Apify
apify.com › zsoftware › json-to-yaml-converter › api › openapi
JSON To YAML Converter OpenAPI definition · Apify
July 6, 2025 - Learn how to interact with JSON To YAML Converter in OpenAPI. Includes an OpenAPI example code snippet for you to get started quickly.
🌐
GitHub
github.com › cebe › php-openapi
GitHub - cebe/php-openapi: Read and write OpenAPI yaml/json files and make the content accessible in PHP objects. · GitHub
$mode = ReferenceContext::RESOLVE_MODE_INLINE; // an absolute URL or file path is needed to allow resolving external references $openapi = Reader::readFromJsonFile('https://www.example.com/api/openapi.json', OpenAPI::class, $mode); $openapi ...
Starred by 498 users
Forked by 98 users
Languages   PHP 98.2% | Makefile 1.1% | Dockerfile 0.7%
🌐
SmartBear
support.smartbear.com › swaggerhub › docs › tutorials › openapi-3-tutorial.html
OpenAPI 3.0 Tutorial | SwaggerHub Documentation
September 18, 2023 - openapi: 3.0.0 info: version: 1.0.0 title: Simple API description: A simple API to illustrate OpenAPI concepts servers: - url: https://example.io/v1 security: - BasicAuth: [] paths: /artists: get: description: Returns a list of artists parameters: # ----- Added line ------------------------------------------ - $ref: '#/components/parameters/PageLimit' - $ref: '#/components/parameters/PageOffset' # ---- /Added line ------------------------------------------ responses: '200': description: Successfully returned a list of artists content: application/json: schema: type: array items: # ----- Added
🌐
OpenAPI
spec.openapis.org › oas › v3.2.0.html
OpenAPI Specification v3.2.0
Note that since this example is written in YAML, the Example Object’s value field can be formatted as YAML due to the trivial conversion to JSON.