yamljsonconversiondevops

YAML to JSON: Complete Conversion Guide with Examples

·7 min read

YAML vs JSON — Which to Use?

YAML (YAML Ain't Markup Language) and JSON solve the same problem — representing structured data — but for different audiences:

FeatureYAMLJSON
Human readabilityHigh — no brackets, indent-basedModerate — explicit braces
CommentsSupported (#)Not supported
API payloadsRarelyUniversally
Config filesVery commonLess common
Strict parsingComplexSimpler

YAML is used in configuration (Kubernetes, Docker Compose, GitHub Actions, Ansible). JSON is used in APIs, databases, and serialization. Knowing how to convert between them is essential for modern DevOps and backend work.

YAML to JSON: Type Mapping

yaml
name: Ravi Kumar       # → "Ravi Kumar" (string)
age: 28                # → 28 (number)
active: true           # → true (boolean)
score: null            # → null
tags:
  - developer          # → ["developer", "typescript"]
  - typescript

Output:

json
{
  "name": "Ravi Kumar",
  "age": 28,
  "active": true,
  "score": null,
  "tags": ["developer", "typescript"]
}

YAML Boolean Aliases

YAML supports many aliases for boolean values that JSON does not:

  • true: true, True, TRUE, yes, Yes, YES, on, On, ON
  • false: false, False, FALSE, no, No, NO, off, Off, OFF

These all become true or false in JSON. Be careful — "yes" in YAML becomes true in JSON, which can cause surprises when the value is actually meant to be the string "yes".

Handling Comments

YAML supports inline and full-line comments with #:

yaml
port: 8080  # default port
host: localhost
# debug mode — disable in production
debug: false

When converting to JSON, all comments are stripped — JSON has no comment syntax. If you need to preserve notes, move them into a separate documentation field.

Nested Structures

yaml
database:
  host: localhost
  port: 5432
  credentials:
    user: admin
    password: secret

Becomes:

json
{
  "database": {
    "host": "localhost",
    "port": 5432,
    "credentials": {
      "user": "admin",
      "password": "secret"
    }
  }
}

Common Use Cases

Kubernetes manifests: k8s accepts both YAML and JSON. Convert YAML manifests to JSON for use with the API directly or audit tools.

OpenAPI / Swagger: Many tools accept both formats. Convert your YAML spec to JSON for tools that require it.

GitHub Actions: Workflow files are YAML. Convert to JSON to analyze them programmatically.

Ansible playbooks: Ansible uses YAML. Convert to JSON for integration with REST-based orchestration tools.

Limitations to Know

YAML anchors and aliases (&anchor / *alias) are a YAML-only feature — they are not supported in JSON. Resolve them manually before converting.

Multi-document YAML (---) separates multiple documents in one file. Most converters only process the first document.

Block scalars (| and >) are multi-line string features in YAML. Basic converters may not handle these perfectly.

Use JSONKit's YAML to JSON tool for instant browser-based conversion — paste any YAML config and get clean JSON in one click.

Try YAML to JSON

Convert YAML config files to JSON objects, ready for APIs or databases.