Convert OpenAPI/Swagger JSON to YAML [Complete Guide]

Convert OpenAPI and Swagger JSON to YAML using online tools, Swagger Editor, or command-line utilities. YAML is often preferred for OpenAPI specs because of its readability and support for comments. Both formats are officially supported by the OpenAPI specification, making conversion straightforward and lossless. See our YAML vs JSON comparison for more details.

Why Use YAML for OpenAPI?

Many developers prefer YAML over JSON for OpenAPI specifications. Here's why the Swagger JSON vs YAML debate often favors YAML:

  • Comments: YAML supports comments (#), perfect for documenting API design decisions
  • Readability: Cleaner syntax without brackets and braces
  • Manual editing: Easier to write and modify by hand
  • Industry standard: Most OpenAPI examples and tutorials use YAML
Note: The OpenAPI specification (versions 2.0, 3.0, and 3.1) officially supports both JSON and YAML. Your choice depends on workflow and tooling preferences. For CLI conversion, see our command-line guide.

Method 1: Online Converter (Recommended)

The fastest way to convert Swagger JSON to YAML is using our free online converter:

  1. Open the converter: Navigate to our JSON to YAML converter
  2. Paste your OpenAPI JSON: Copy your entire OpenAPI/Swagger specification
  3. Click Convert: The tool instantly converts to YAML
  4. Download or copy: Save the YAML file or copy to clipboard

Example: OpenAPI 3.0 Conversion

Input (JSON):

{
  "openapi": "3.0.0",
  "info": {
    "title": "Pet Store API",
    "version": "1.0.0",
    "description": "A sample API for pet store"
  },
  "paths": {
    "/pets": {
      "get": {
        "summary": "List all pets",
        "responses": {
          "200": {
            "description": "A list of pets"
          }
        }
      }
    }
  }
}

Output (YAML):

openapi: 3.0.0
info:
  title: Pet Store API
  version: 1.0.0
  description: A sample API for pet store
paths:
  /pets:
    get:
      summary: List all pets
      responses:
        '200':
          description: A list of pets

Method 2: Swagger Editor

The official Swagger Editor can convert between JSON and YAML while also validating your OpenAPI specification:

  1. Go to editor.swagger.io
  2. Click File → Import File or paste your JSON
  3. The editor automatically displays as YAML
  4. Click File → Save as YAML to download
Bonus: Swagger Editor validates your spec during conversion, highlighting any errors or warnings in real-time.

Method 3: Command Line

For automation and CI/CD pipelines, use command-line tools to convert OpenAPI JSON to YAML:

Using yq

# Install yq
brew install yq  # macOS
sudo apt-get install yq  # Ubuntu

# Convert OpenAPI JSON to YAML
yq -P openapi.json > openapi.yaml

# With specific indent (2 spaces is OpenAPI standard)
yq -P --indent 2 openapi.json > openapi.yaml

Using Python

# One-liner conversion
python -c "import json,yaml,sys; print(yaml.dump(json.load(open('openapi.json')), default_flow_style=False))" > openapi.yaml

Using Node.js

# Install js-yaml
npm install -g js-yaml

# Convert (using npx)
npx js-yaml openapi.json > openapi.yaml

OpenAPI JSON vs YAML Comparison

When deciding between OpenAPI JSON vs YAML, consider these factors:

Aspect JSON YAML
Swagger Editor ✅ Supported ✅ Supported
Comments ❌ Not allowed ✅ Supported (#)
File size ✅ Smaller Larger (no braces)
Human editing Harder (syntax strict) ✅ Easier
Programmatic generation ✅ Easier Requires library
Industry preference API gateways ✅ Documentation
Parsing speed ✅ Faster Slower

Recommendation: Use YAML for manual editing and documentation. Use JSON for programmatic generation and API gateways.

Convert OpenAPI YAML to JSON

Need to convert Swagger YAML to JSON? Here's how to reverse the process:

# Using yq
yq -o json openapi.yaml > openapi.json

# Using Python
python -c "import json,yaml; print(json.dumps(yaml.safe_load(open('openapi.yaml')), indent=2))" > openapi.json

# Online: Use our YAML to JSON converter
# Simply paste YAML and convert to JSON
Warning: Comments in YAML will be lost when converting to JSON, as JSON doesn't support comments.

Best Practices

For OpenAPI YAML Files

  • Use 2-space indentation: This is the OpenAPI standard
  • Add comments: Document API design decisions with # comments
  • Quote strings when needed: Avoid YAML parsing issues
  • Validate after conversion: Use Swagger Editor or spectral

For CI/CD Pipelines

# GitHub Actions example: Validate & Convert OpenAPI
- name: Convert OpenAPI JSON to YAML
  run: |
    yq -P api/openapi.json > api/openapi.yaml
    
- name: Validate OpenAPI spec
  run: |
    npx @stoplight/spectral-cli lint api/openapi.yaml

Frequently Asked Questions

Should I use JSON or YAML for OpenAPI?

YAML is generally preferred for OpenAPI specifications because it supports comments, is more readable, and is easier to edit manually. However, JSON is better for programmatic generation and has smaller file sizes. Both formats are officially supported.

How do I convert Swagger JSON to YAML?

Use our free online converter, Swagger Editor (editor.swagger.io), or the yq command line tool. In Swagger Editor, paste your JSON, then go to File → Save as YAML. For CLI, run: yq -P openapi.json > openapi.yaml

Is OpenAPI 3.0 compatible with both JSON and YAML?

Yes, OpenAPI 3.0 and 3.1 specifications officially support both JSON and YAML formats. The specification content is identical—only the syntax differs. You can convert between formats without losing any functionality.

Can I add comments to OpenAPI YAML?

Yes, YAML supports comments using the # symbol, which is a major advantage over JSON for OpenAPI specs. Comments help document API design decisions and are preserved in YAML files but would be lost if you convert back to JSON.

Need to convert OpenAPI JSON to YAML?

Use our free online converter—works with any OpenAPI or Swagger specification.

Open Converter Tool →