Chapters: JavaScript HTML & CSS JSON XML YAML ← Full TOC

← YAML Index

⚖️ YAML vs JSON vs TOML — Compare data formats — when to use which

YAML, JSON, TOML, and XML each have different strengths. YAML excels at human-written config. JSON excels at machine-to-machine APIs. TOML excels at application config files. XML excels at document-centric data with mixed content.

📋 Reference

ItemSyntax / ValueDescription
YAML comments# comment anywhereJSON has no comments — YAML wins for config files
YAML multi-line| and > block scalarsJSON requires \n escapes — YAML is far more readable
YAML anchors& and * for reuseJSON has no reuse mechanism — copy/paste only
YAML typesauto-detects dates, booleans, nullsJSON has 6 explicit types — more predictable
YAML ambiguityyes/no/on/off parsed as boolean in 1.1JSON booleans are always true/false — unambiguous
YAML securityNever yaml.load() untrusted inputJSON.parse() is safe — YAML can execute code
YAML toolingLess universal than JSONJSON has universal library support in every language
JSON compactnessNo whitespace requiredYAML requires indentation — more verbose
JSON APIsStandard for REST APIsYAML not commonly used for API responses
JSON in YAMLAll valid JSON is valid YAMLMigration path: JSON → YAML is always valid
TOML sections[section] / [[array-of-tables]]Clear section syntax — better than YAML for app config
TOML typesExplicit: 2026-01-01 = date literalTOML has native date/time types
TOML use caseCargo.toml, pyproject.toml, config.tomlTOML is Rust/Python ecosystem standard
XML verbosity<tag>value</tag> much more verboseJSON/YAML far more compact for data
XML mixed content<p>Text with <em>emphasis</em></p>XML handles mixed text/element content
XML namespacesPrevent name collisionsNo equivalent in JSON/YAML
XML transformsXSLT — powerful transformationNo equivalent in JSON/YAML
Use JSON whenMachine-to-machine APIs, web responsesUniversal, fast, unambiguous
Use YAML whenConfig files, CI/CD, KubernetesHuman-readable, supports comments and anchors
Use TOML whenApplication config files (.toml)Clear, unambiguous, typed
Use XML whenDocument data, enterprise integrationNamespaces, mixed content, XSLT transforms
yq toolyq '.key' file.yamlLike jq but for YAML — process YAML on command line

💡 Example

# ── Same data in YAML, JSON, TOML, and XML ──────────────────

# YAML (config/DevOps best choice — human-written)
---
database:
  host:     localhost
  port:     5432
  name:     mywebuniversity
  username: admin
  pool:
    min: 2
    max: 10
  ssl:
    enabled: true
    verify:  true

server:
  host:    0.0.0.0
  port:    8080
  workers: 4
  timeout: 30
  cors:
    origins: [https://mywebuniversity.com, http://localhost:3000]
    methods: [GET, POST, PUT, DELETE]

logging:
  level:  info
  format: json
  file:   /var/log/app.log

# ── Equivalent JSON (API responses — universal) ──────────────
{
  "database": {
    "host": "localhost",
    "port": 5432,
    "name": "mywebuniversity",
    "username": "admin",
    "pool": { "min": 2, "max": 10 },
    "ssl": { "enabled": true, "verify": true }
  },
  "server": {
    "host": "0.0.0.0",
    "port": 8080,
    "workers": 4,
    "timeout": 30,
    "cors": {
      "origins": ["https://mywebuniversity.com", "http://localhost:3000"],
      "methods": ["GET", "POST", "PUT", "DELETE"]
    }
  },
  "logging": { "level": "info", "format": "json", "file": "/var/log/app.log" }
}

# ── Equivalent TOML (application config best choice) ─────────
[database]
host     = "localhost"
port     = 5432
name     = "mywebuniversity"
username = "admin"

[database.pool]
min = 2
max = 10

[database.ssl]
enabled = true
verify  = true

[server]
host    = "0.0.0.0"
port    = 8080
workers = 4
timeout = 30

[server.cors]
origins = ["https://mywebuniversity.com", "http://localhost:3000"]
methods = ["GET", "POST", "PUT", "DELETE"]

[logging]
level  = "info"
format = "json"
file   = "/var/log/app.log"

# ── yq command-line tool ─────────────────────────────────────
# Install: snap install yq  /  brew install yq  /  wget yq binary

yq '.database.host'              config.yaml       # extract value
yq '.server.port = 9090'         config.yaml       # update value
yq '.server.workers'             config.yaml       # get workers
yq 'del(.logging)'               config.yaml       # delete key
yq '.database | keys'            config.yaml       # list keys
yq -o json '.'                   config.yaml       # convert to JSON
yq -P '.'                        config.json       # convert JSON to YAML (pretty)
yq eval-all 'select(fileIndex==0) * select(fileIndex==1)' base.yaml override.yaml  # merge files

← YAML in DevOps  |  🏠 Index