The Swagger Inflector library has the ExampleBuilder class exactly for this purpose. It lets you generate JSON, XML and YAML examples from models in an OpenAPI (Swagger) definition.

OpenAPI 2.0 example

To work with OpenAPI 2.0 (swagger: '2.0') definitions, use Swagger Java libraries 1.x.

import io.swagger.parser.SwaggerParser;
import io.swagger.models.*;
import io.swagger.inflector.examples.*;
import io.swagger.inflector.examples.models.Example;
import io.swagger.inflector.processors.JsonNodeExampleSerializer;
import io.swagger.util.Json;
import io.swagger.util.Yaml;
import java.util.Map;
import com.fasterxml.jackson.databind.module.SimpleModule;

...

// Load your OpenAPI/Swagger definition
Swagger swagger = new SwaggerParser().read("http://petstore.swagger.io/v2/swagger.json");

// Create an Example object for the Pet model
Map<String, Model> definitions = swagger.getDefinitions();
Model pet = definitions.get("Pet");
Example example = ExampleBuilder.fromModel("Pet", pet, definitions, new HashSet<String>());
// Another way:
// Example example = ExampleBuilder.fromProperty(new RefProperty("Pet"), swagger.getDefinitions());

// Configure example serializers
SimpleModule simpleModule = new SimpleModule().addSerializer(new JsonNodeExampleSerializer());
Json.mapper().registerModule(simpleModule);
Yaml.mapper().registerModule(simpleModule);

// Convert the Example object to string

// JSON example
String jsonExample = Json.pretty(example);
System.out.println(jsonExample);

// YAML example
String yamlExample = Yaml.pretty().writeValueAsString(example);
System.out.println(yamlExample);

// XML example (TODO: pretty-print it)
String xmlExample = new XmlExampleSerializer().serialize(example);
System.out.println(xmlExample);

OpenAPI 3.0 example

For an OpenAPI 3.0 example, see this answer. You need version 2.x of Swagger Java libraries, and update the imports and class names appropriately, e.g. change io.swagger.parser.SwaggerParser to io.swagger.v3.parser.OpenAPIV3Parser and so on.

Answer from Helen on Stack Overflow
🌐
DEV Community
dev.to › ziishaned › getting-started-with-swagger-3bbc
Writing a swagger.json file - DEV Community
May 1, 2025 - Now go back to our swagger.json file and replace "responses": {} with the following text: "responses": { "200": { "description": "Successfully fetched all posts from JSONPlaceholder", "content": { "application/json": { "schema": { "type": "array", "items": { "type": "object", "properties": { "userId": { "type": "number" }, "id": { "type": "number" }, "title": { "type": "string" }, "body": { "type": "string" } }, "example": { "userId": 1, "id": 1, "title": "sunt aut facere repellat provident occaecati excepturi optio reprehenderit", "body": "quia et suscipit\nsuscipit recusandae consequuntur expedita et cum\nreprehenderit molestiae ut ut quas totam\nnostrum rerum est autem sunt rem eveniet architecto" } } } } } } }
🌐
Swagger
swagger.io › docs › specification › v2_0 › basic-structure
Basic Structure | Swagger Docs
The global definitions section lets you define common data structures used in your API. They can be referenced via $refwhenever a schema is required – both for request body and response body. For example, this JSON object:
Discussions

How to generate JSON examples from OpenAPI/Swagger model definition? - Stack Overflow
This can result in 'malformed' JSON. You can work around this by creating your own StringProperty objects but that kind of defeats the purposes of having a builder. 2019-08-14T03:33:03.987Z+00:00 ... @Joman68 you can open an issue at github.com/swagger-api/swagger-inflector/issues (or, better yet, submit a PR). As a workaround you can modify your API definition to provide a custom example ... More on stackoverflow.com
🌐 stackoverflow.com
How to generate a Swagger #definition from sample JSON - Stack Overflow
Take the following #definition from the pet store example. Given a #definition section a JSON structure can be generated ... Given the below JSON Structure can I get the #defintion section of a swagger file generated to save some typing More on stackoverflow.com
🌐 stackoverflow.com
How do you create Swagger jsons for your API?
I used swagger annotations in my previous project and it was total crap. (In general I consider quite a lot of swagger tooling done by its authors to be of poor quality). Currently I am for using some API describing algebra that you can easily turn into client/server/documentation: see https://github.com/julienrf/endpoints https://github.com/softwaremill/tapir and similar. More on reddit.com
🌐 r/scala
18
10
May 24, 2019
Swagger for Django api
Django ninja is pretty nice in this respect. And imo nicer to use than DRF. More on reddit.com
🌐 r/django
9
4
April 24, 2023
🌐
GitHub
gist.github.com › lenage › 08964335de9064540c8c335fb849c5da
swagger JSON example · GitHub
swagger JSON example · Raw · feature.swagger.json · This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
🌐
Tibco
docs.tibco.com › pub › om-ll › 5.0.0 › doc › html › GUID-A6FB66EA-2474-4A00-BC78-E4487300BAA9.html
Sample Swagger JSON File
For a complete list of all the objects and fields that can be defined in the swagger.json file, refer to https://github.com/OAI/OpenAPI-Specification/blob/OpenAPI.next/versions/3.0.0.md#specification.
🌐
Apidog
apidog.com › blog › generate-swagger-documentation-json
How to Generate Swagger Documentation from JSON
February 3, 2026 - You may encounter scenarios where you need to generate Swagger documentation from existing JSON or YAML files. In this post, we will provide a detailed guide on how to generate Swagger documentation from JSON, complete with examples and step-by-step instructions.
Top answer
1 of 4
11

The Swagger Inflector library has the ExampleBuilder class exactly for this purpose. It lets you generate JSON, XML and YAML examples from models in an OpenAPI (Swagger) definition.

OpenAPI 2.0 example

To work with OpenAPI 2.0 (swagger: '2.0') definitions, use Swagger Java libraries 1.x.

import io.swagger.parser.SwaggerParser;
import io.swagger.models.*;
import io.swagger.inflector.examples.*;
import io.swagger.inflector.examples.models.Example;
import io.swagger.inflector.processors.JsonNodeExampleSerializer;
import io.swagger.util.Json;
import io.swagger.util.Yaml;
import java.util.Map;
import com.fasterxml.jackson.databind.module.SimpleModule;

...

// Load your OpenAPI/Swagger definition
Swagger swagger = new SwaggerParser().read("http://petstore.swagger.io/v2/swagger.json");

// Create an Example object for the Pet model
Map<String, Model> definitions = swagger.getDefinitions();
Model pet = definitions.get("Pet");
Example example = ExampleBuilder.fromModel("Pet", pet, definitions, new HashSet<String>());
// Another way:
// Example example = ExampleBuilder.fromProperty(new RefProperty("Pet"), swagger.getDefinitions());

// Configure example serializers
SimpleModule simpleModule = new SimpleModule().addSerializer(new JsonNodeExampleSerializer());
Json.mapper().registerModule(simpleModule);
Yaml.mapper().registerModule(simpleModule);

// Convert the Example object to string

// JSON example
String jsonExample = Json.pretty(example);
System.out.println(jsonExample);

// YAML example
String yamlExample = Yaml.pretty().writeValueAsString(example);
System.out.println(yamlExample);

// XML example (TODO: pretty-print it)
String xmlExample = new XmlExampleSerializer().serialize(example);
System.out.println(xmlExample);

OpenAPI 3.0 example

For an OpenAPI 3.0 example, see this answer. You need version 2.x of Swagger Java libraries, and update the imports and class names appropriately, e.g. change io.swagger.parser.SwaggerParser to io.swagger.v3.parser.OpenAPIV3Parser and so on.

2 of 4
3

Just put your model in https://json-schema-faker.js.org/ and off you go.

Your provided schema works as is with minor modifications: remove "Pets" and add definitions for "Category" and "Tag". See below as an example.

Then click "Generate" and you get fake data back. It appears that this can all be done programmatically via the libraries if you don't want to go through the website (haven't tried that myself though).

{
  "definitions": {
    "description": "making this up so all refs resolve",
    "Category": {
      "type": "string"
    },
    "Tag": {
      "type": "string"
    }
  },
  "comment": "From here on down, it's exactly the same as the OP schema",
  "type": "object",
  "required": [
    "name",
    "photoUrls"
  ],
  "properties": {
    "id": {
      "type": "integer",
      "format": "int64"
    },
    "category": {
      "$ref": "#/definitions/Category"
    },
    "name": {
      "type": "string",
      "example": "doggie"
    },
    "photoUrls": {
      "type": "array",
      "items": {
        "type": "string"
      }
    },
    "tags": {
      "type": "array",
      "items": {
        "$ref": "#/definitions/Tag"
      }
    },
    "status": {
      "type": "string",
      "description": "pet status in the store"
    }
  }
}
Find elsewhere
🌐
Swagger
swagger.io › docs › specification › v2_0 › adding-examples
Adding Examples | Swagger Docs
Swagger allows examples on the response level, each example corresponding to a specific MIME type returned by the operation. Such as one example for application/json, another one for text/csv and so on.
🌐
Google Groups
groups.google.com › g › swagger-swaggersocket › c › VXBeLwh8l48
Sample Swagger 2.0 json/yaml files
Hi Ron, Thank you, those examples helped. As per the swagger spec, defined in https://github.com/swagger-api/swagger-spec/blob/master/versions/2.0.md#file-structure, the references can be in different json files and can be linked them with $ref token [the schema for a request payload (for body parameter) & schema of response objects] ) Do you have any examples for that?
🌐
Blazemeter
blazemeter.com › blog › swagger-editor
Create Your First OpenAPI Definition with Swagger Editor Online | Perforce BlazeMeter
Finally, let’s add a description of the response, so the readers of our Swagger Editor documentation can expect what the output of the API will be even before sending their request. Once again, here goes the full snippet for the paths section: paths: /: get: summary: Get client IP parameters: - in: query name: format type: string description: 'The format to return the response in, i.e. json.' default: json responses: '200': description: Success response schema: type: object properties: ip: type: string example: 88.68.10.107
🌐
Swagger
petstore.swagger.io
Swagger UI
We cannot provide a description for this page right now
🌐
HCL Software
help.hcl-software.com › commerce › 9.1.0 › restapi › tasks › twv_createswagger.html
Creating a Swagger-specification JSON file
You need to determine several things before creating a new JSON file. What are the parameters you will use (described here: http://swagger.io/specification/#parameterObject)?
🌐
Citrusframework
citrusframework.org › samples › swagger
Swagger auto generated sample
{ "swagger": "2.0", "info": { "description": "REST API for todo application", "version": "2.0", "title": "TodoList API", "license": { "name": "Apache License Version 2.0" } }, "host": "localhost:8080", "basePath": "/", "paths": { "/api/todolist": { "get": { "summary": "List todo entries", "description": "Returns all available todo entries.", "operationId": "listTodoEntries", "consumes": [ "application/json" ], "produces": [ "*/*" ], "responses": { "200": { "description": "OK", "schema": { "type": "array", "items": { "$ref": "#/definitions/TodoEntry" } } } } }, "post": { "tags": [ "todo-list-co
🌐
Gitbooks
tomjohnson1492.gitbooks.io › documenting-rest-apis › content › publishingapis › pubapis_swagger.html
Swagger tutorial · Documenting REST APIs - tomjohnson1492
Swagger can be rendered into different visual displays based on the visual framework you decide to use to parse the Swagger spec. There are three resources: pet, store, and user. In the Pet resource, expand the Post method. Click the yellow JSON in the Model ...
🌐
GitHub
github.com › VictorianGovernment › api-design-standards › blob › master › api-example-swagger-v1.4.json
api-design-standards/api-example-swagger-v1.4.json at master · VictorianGovernment/api-design-standards
This endpoint can\n provide information in the following formats by using the `outputformat`\n query parameter.\n\n - application/json # Replace with your output format\n - application/atom+xml # Replace with your output format\n\n\n ## Extra Developer Documentation:\n If you have more developer documentation or tutorials specific to helping\n developers interact with this endpoint.\n **url:** 'https://example.com/documentation/link'\n\n# Uptime & Planned Outages\nLorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod\ntempor incididunt ut labore et dolore magna aliqua.
Author   VictorianGovernment
🌐
SmartBear
support.smartbear.com › swaggerhub › docs › en › get-started › openapi-2-0-tutorial.html
Swagger (OpenAPI 2.0) Tutorial | SwaggerHub Documentation
In this tutorial, we will write an API definition in the Swagger 2.0 format (also known as OpenAPI 2.0). Swagger can be written in JSON or YAML, though we recommend writing it in YAML, because it is easier to read and understand. ... swagger: '2.0' info: version: 1.0.0 title: Simple example ...
🌐
Microsoft Learn
learn.microsoft.com › en-us › aspnet › core › tutorials › web-api-help-pages-using-swagger
ASP.NET Core web API documentation with Swagger / OpenAPI | Microsoft Learn
February 23, 2026 - This tutorial provides a walkthrough of adding Swagger to generate documentation and help pages for a web API app.
🌐
Tibco
docs.tibco.com › pub › amsg › 3.4.1 › doc › html › GUID-39BA8E32-B96F-4BD8-A6C3-E950F46A98E5.html
Overview of the Swagger JSON File
For a complete list of all the objects and fields that can be defined in the swagger.json file, refer to https://github.com/OAI/OpenAPI-Specification/.
🌐
Rackerlabs
rackerlabs.github.io › wadl2swagger › openstack.html
Sample Swagger files
Below is a list of WADL files and the equivalent Swagger files that were generated with wadl2swagger.
🌐
GitHub
github.com › RicoSuter › swagger-spec › blob › master › examples › v2.0 › json › petstore-simple.json
swagger-spec/examples/v2.0/json/petstore-simple.json at master · RicoSuter/swagger-spec
"description": "A sample API that uses a petstore as an example to demonstrate features in the swagger-2.0 specification", "termsOfService": "http://swagger.io/terms/", "contact": { "name": "Swagger API Team" }, "license": { "name": "MIT" } }, "host": "petstore.swagger.io", "basePath": "/api", "schemes": [ "http" ], "consumes": [ "application/json" ], "produces": [ "application/json" ], "paths": { "/pets": { "get": { "description": "Returns all pets from the system that the user has access to", "operation
Author   RicoSuter