YAML vs JSON

YAML and JSON are both popular data formats used to store and exchange structured information. Both represent the same kinds of data — strings, numbers, booleans, lists, and objects. However, they have significant differences in syntax, readability, and use cases. Understanding these differences helps in choosing the right format for the right job.

The Core Relationship Between YAML and JSON

YAML 1.2 is a superset of JSON. This means every valid JSON document is also a valid YAML document. A YAML parser can read JSON. The reverse is not true — JSON parsers cannot read YAML-specific features like comments or anchors.

+----------------------------------+
|         YAML (superset)          |
|   +---------------------------+  |
|   |      JSON (subset)        |  |
|   +---------------------------+  |
+----------------------------------+

Side-by-Side Comparison – Same Data

In JSON

{
  "name": "Alice",
  "age": 28,
  "active": true,
  "skills": ["Python", "YAML", "Docker"],
  "address": {
    "city": "Mumbai",
    "country": "India"
  }
}

In YAML

name: Alice
age: 28
active: true
skills:
  - Python
  - YAML
  - Docker
address:
  city: Mumbai
  country: India

Both represent identical data. YAML uses fewer characters, no quotes for simple strings, no curly braces, no commas, and is significantly easier to read.

Key Differences Table

FeatureYAMLJSON
CommentsSupported with #Not supported
Quotes for stringsOptional for simple stringsAlways required
Multiline stringsSupported (| and >)Not natively supported
Anchors and aliasesSupportedNot supported
Curly braces / bracketsOptional (flow style only)Mandatory
Commas between itemsNot neededRequired
IndentationMandatory (spaces only)Optional (cosmetic only)
Human readabilityEasier to readHarder to read for large files
Parsing complexityMore complex to parseSimpler to parse
Language supportAll major languagesAll major languages
Data typesAuto-detected, more typesExplicit, fewer types
Multiple documentsOne file, multiple docs (---)One document per file

Comments – YAML Wins

JSON has no comment support. This is a known limitation that many developers find frustrating. Configuration files benefit greatly from comments that explain values. YAML supports comments natively.

# YAML – with comments
port: 8080   # Change to 443 for HTTPS

// JSON – no comments allowed (this is invalid JSON)
{ "port": 8080 }

Multiline Strings – YAML Wins

# YAML – clean multiline
script: |
  echo "Hello"
  echo "World"

# JSON – ugly workaround
{ "script": "echo \"Hello\"\necho \"World\"" }

YAML's literal block style makes embedding scripts, SQL, or HTML far cleaner than JSON's escape-heavy approach.

Parsing and Machine Use – JSON Wins

JSON is easier and faster to parse programmatically. Its strict syntax — always quoted keys, always explicit braces — removes ambiguity. This is why JSON is preferred for APIs and data exchange between systems. YAML's flexibility (optional quotes, implicit types) means parsers must handle more edge cases.

Where Each Format Is Used

Use CasePreferred FormatReason
REST APIsJSONUniversal support, fast parsing
Config files (DevOps)YAMLReadable, supports comments
GitHub Actions / CI/CDYAMLBetter multiline support
KubernetesYAML (and JSON)Both supported, YAML preferred
Package.json (Node.js)JSONStandard format for NPM
OpenAPI / Swagger specsBothBoth formats accepted
Data storage / databasesJSONBetter tool and query support

Converting YAML to JSON

Since YAML is a superset of JSON, converting between them is straightforward using programming libraries or online tools.

# YAML Input
person:
  name: Alice
  age: 28

# Equivalent JSON Output
{
  "person": {
    "name": "Alice",
    "age": 28
  }
}

Potential YAML Pitfalls That JSON Avoids

  • Implicit type conversion: YAML may read yes as a boolean. JSON never does this.
  • Indentation errors: A wrong indent in YAML breaks parsing. JSON indentation is cosmetic only.
  • Tab vs space errors: YAML rejects tabs. JSON allows any whitespace.
  • Duplicate keys: JSON defines this as undefined behavior. YAML also discourages it but some parsers accept it.

Quick Decision Guide

+-------------------------------+
|  Choose YAML when...          |
+-------------------------------+
|  - Writing config files       |
|  - Human readability matters  |
|  - Comments are needed        |
|  - Working with DevOps tools  |
+-------------------------------+

+-------------------------------+
|  Choose JSON when...          |
+-------------------------------+
|  - Building APIs              |
|  - Parsing speed matters      |
|  - Data interchange between   |
|    different systems          |
|  - JavaScript is involved     |
+-------------------------------+

Summary

  • YAML is a superset of JSON — all valid JSON is valid YAML
  • YAML is more readable and supports comments, multiline strings, and anchors
  • JSON is simpler to parse and preferred for APIs and machine-to-machine data exchange
  • YAML is the dominant format for DevOps configuration files
  • JSON is the dominant format for web APIs and JavaScript ecosystems
  • Both formats represent the same underlying data structures

Leave a Comment