Skip to main content
The YAML to JSON tool converts YAML (YAML Ain’t Markup Language) documents into JSON format. It handles both single-document and multi-document YAML files, making it essential for working with configuration files, CI/CD pipelines, and data interchange.

Use Cases

  • Converting Kubernetes manifests to JSON for programmatic processing
  • Transforming CI/CD configs (GitHub Actions, GitLab CI) to JSON
  • Parsing YAML config files for API consumption
  • Validating YAML syntax by converting to JSON
  • Processing multi-document YAML (e.g., Kubernetes resources with --- separators)
  • Data interchange between YAML-based and JSON-based systems

How It Works

The tool uses the js-yaml library to parse YAML syntax and convert it to JavaScript objects, then serializes to JSON:
  1. Parse YAML using yamlLoadAll() (supports multi-document)
  2. Detect document count:
    • Single document → Output single JSON object
    • Multiple documents → Output JSON array of objects
  3. Serialize to JSON with 2-space indentation
The tool automatically detects multi-document YAML (documents separated by ---) and outputs a JSON array when multiple documents are found.

Input Format

Single-Document YAML

name: John Doe
age: 30
address:
  street: 123 Main St
  city: New York
  zip: "10001"
hobby:
  - reading
  - hiking
  - coding

Multi-Document YAML

---
kind: Deployment
metadata:
  name: app-deployment
spec:
  replicas: 3
---
kind: Service
metadata:
  name: app-service
spec:
  type: ClusterIP
  ports:
    - port: 80

Output Format

Single Document → JSON Object

{
  "name": "John Doe",
  "age": 30,
  "address": {
    "street": "123 Main St",
    "city": "New York",
    "zip": "10001"
  },
  "hobby": [
    "reading",
    "hiking",
    "coding"
  ]
}

Multiple Documents → JSON Array

[
  {
    "kind": "Deployment",
    "metadata": {
      "name": "app-deployment"
    },
    "spec": {
      "replicas": 3
    }
  },
  {
    "kind": "Service",
    "metadata": {
      "name": "app-service"
    },
    "spec": {
      "type": "ClusterIP",
      "ports": [
        { "port": 80 }
      ]
    }
  }
]

Examples

Input:
app:
  name: MyApp
  version: 1.2.3
  debug: true
  features:
    - auth
    - api
    - admin

Output:
{
  "app": {
    "name": "MyApp",
    "version": "1.2.3",
    "debug": true,
    "features": ["auth", "api", "admin"]
  }
}

Technical Details

Located in lib/tools/engine.ts:497-501
The tool uses js-yaml (YAML 1.2 parser):
import { loadAll as yamlLoadAll } from 'js-yaml';

const docs = yamlLoadAll(input);
if (docs.length === 1) {
  return { output: JSON.stringify(docs[0], null, 2) };
}
return { output: JSON.stringify(docs, null, 2) };

YAML Features Supported

  • Scalars: strings, numbers, booleans, null
  • Collections: sequences (arrays), mappings (objects)
  • Multi-line strings: literal (|) and folded (>) blocks
  • Anchors and aliases: &anchor and *alias references
  • Comments: Parsed but not preserved in JSON output
  • Multi-document files: Separated by ---

Type Conversion

YAMLJSON
true, false, yes, notrue, false
123, 0x1A, 0o17123 (decimal)
1.23, 1.23e4, .inf1.23, 12300, null
null, ~null
"quoted", unquoted"string"
2024-03-04"2024-03-04" (string)
YAML date objects (e.g., 2024-03-04) are converted to ISO 8601 strings in JSON, not Date objects. YAML binary data (!!binary) is converted to base64 strings.

YAML Syntax Tips

Preserving Numeric Strings

Quote numbers that should remain strings:
zip_code: "10001"  # String, not number
port: 8080         # Number

Multi-line Strings

# Literal block (preserves newlines)
description: |
  Line 1
  Line 2
  Line 3

# Folded block (collapses newlines to spaces)
summary: >
  This is a long
  sentence that will
  be on one line.

Anchors and Aliases

defaults: &defaults
  timeout: 30
  retries: 3

service1:
  <<: *defaults
  name: Service 1

service2:
  <<: *defaults
  name: Service 2
Converts to:
{
  "defaults": {"timeout": 30, "retries": 3},
  "service1": {"timeout": 30, "retries": 3, "name": "Service 1"},
  "service2": {"timeout": 30, "retries": 3, "name": "Service 2"}
}

Common Errors

YAML is indentation-sensitive (like Python). Use consistent spaces (2 or 4) for indentation. Do not mix tabs and spaces.
# ❌ Incorrect (mixed indentation)
parent:
    child: value

# ✅ Correct (consistent 2-space indentation)
parent:
  child: value
Ensure colons (:) are followed by a space:
# ❌ Incorrect (no space after colon)
key:value

# ✅ Correct
key: value
The separator --- must be on its own line:
# ❌ Incorrect
doc1: value ---
doc2: value

# ✅ Correct
doc1: value
---
doc2: value

Build docs developers (and LLMs) love