What Is YAML?
YAML (YAML Ain't Markup Language) is a human-friendly data serialization format. It was designed to be easy to read and write by humans while remaining simple for machines to parse. Unlike JSON, which relies on curly braces and brackets, YAML uses indentation and line breaks to represent structure — making it feel closer to a plain text outline than a programming language.
YAML has become the configuration format of choice for DevOps tools, container orchestration, and CI/CD pipelines. If you work with Kubernetes, Docker Compose, GitHub Actions, GitLab CI, Ansible, or Helm, you are writing YAML every day. Understanding how YAML maps to JSON is essential because many APIs and libraries expect JSON, and converting between the two formats is a routine task.
YAML vs JSON: Syntax Comparison
YAML and JSON can represent the same data structures — objects (called mappings in YAML), arrays (called sequences), and scalar values (strings, numbers, booleans, null). The difference is entirely syntactic. Here is the same data in both formats:
YAML Version
# Application configuration
app:
name: PulpMiner
version: 2.4.1
debug: false
server:
host: 0.0.0.0
port: 8080
ssl: true
database:
host: db.example.com
port: 5432
name: pulpminer_prod
credentials:
username: admin
password: secret123
features:
- name: scraping
enabled: true
- name: scheduling
enabled: false
- name: export
enabled: trueJSON Version
{
"app": {
"name": "PulpMiner",
"version": "2.4.1",
"debug": false
},
"server": {
"host": "0.0.0.0",
"port": 8080,
"ssl": true
},
"database": {
"host": "db.example.com",
"port": 5432,
"name": "pulpminer_prod",
"credentials": {
"username": "admin",
"password": "secret123"
}
},
"features": [
{
"name": "scraping",
"enabled": true
},
{
"name": "scheduling",
"enabled": false
},
{
"name": "export",
"enabled": true
}
]
}Notice how the YAML version is more compact and readable. Comments (lines starting with #) are supported in YAML but not in JSON. YAML also does not require quotes around most strings, and boolean values like true and false are inferred automatically.
YAML Indentation Rules
Indentation is the single most important thing to get right in YAML. Incorrect indentation will change the structure of your data or cause a parse error. Here are the rules:
- Use spaces, never tabs. YAML forbids tab characters for indentation. Most editors can be configured to insert spaces when you press Tab.
- Be consistent with indent width. While YAML allows any number of spaces per level, two spaces per level is the convention. Mixing two-space and four-space indentation in the same file will cause errors.
- Nested keys must be indented further than their parent. A child key at the same indentation level as its parent will be treated as a sibling, not a child.
- Sequences use a dash followed by a space. Each item in a list starts with
-(dash-space) at the same indentation level.
Here is an example that illustrates common indentation mistakes:
# CORRECT: child keys are indented under parent
server:
host: localhost
port: 3000
# WRONG: child keys at same level as parent
server:
host: localhost
port: 3000
# CORRECT: list items under the key
fruits:
- apple
- banana
- cherry
# WRONG: mixing indentation widths
database:
host: localhost
port: 5432 # Error: over-indentedReal-World YAML Use Cases
Kubernetes Deployment
Kubernetes manifests are written in YAML. A deployment file defines the desired state of your application — the container image, replica count, resource limits, and more:
apiVersion: apps/v1
kind: Deployment
metadata:
name: web-app
labels:
app: web-app
spec:
replicas: 3
selector:
matchLabels:
app: web-app
template:
metadata:
labels:
app: web-app
spec:
containers:
- name: web-app
image: myregistry/web-app:1.2.0
ports:
- containerPort: 8080
resources:
limits:
memory: "256Mi"
cpu: "500m"
requests:
memory: "128Mi"
cpu: "250m"Docker Compose
Docker Compose uses YAML to define multi-container applications. Converting this to JSON might be needed when using the Docker Engine API directly:
version: "3.8"
services:
web:
build: ./frontend
ports:
- "3000:3000"
environment:
- NODE_ENV=production
depends_on:
- api
api:
build: ./backend
ports:
- "8080:8080"
environment:
- DATABASE_URL=postgres://db:5432/myapp
depends_on:
- db
db:
image: postgres:16
volumes:
- pgdata:/var/lib/postgresql/data
environment:
- POSTGRES_DB=myapp
- POSTGRES_PASSWORD=secret
volumes:
pgdata:GitHub Actions
CI/CD pipelines in GitHub Actions are defined with YAML workflow files. You might convert these to JSON for programmatic generation or validation:
name: CI Pipeline
on:
push:
branches: [main]
pull_request:
branches: [main]
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: "20"
cache: "npm"
- run: npm ci
- run: npm test
- run: npm run buildConverting with Code
If you need to convert YAML to JSON programmatically, here is how to do it in several languages:
JavaScript / Node.js
import { parse } from "yaml";
import { readFileSync, writeFileSync } from "fs";
// Read YAML file
const yamlContent = readFileSync("config.yaml", "utf-8");
// Parse YAML to a JavaScript object
const data = parse(yamlContent);
// Convert to JSON string
const jsonString = JSON.stringify(data, null, 2);
// Write JSON file
writeFileSync("config.json", jsonString);
console.log("Converted successfully!");Python
import yaml
import json
# Read YAML file
with open("config.yaml", "r") as f:
data = yaml.safe_load(f)
# Write JSON file
with open("config.json", "w") as f:
json.dump(data, f, indent=2)
print("Converted successfully!")Command Line (using yq)
# Install yq (https://github.com/mikefarah/yq)
# macOS
brew install yq
# Convert YAML to JSON
yq -o=json config.yaml > config.json
# Convert JSON to YAML
yq -P config.json > config.yaml
# Pipe from stdin
cat config.yaml | yq -o=jsonFor quick one-off conversions without writing code, use PulpMiner's YAML to JSON Converter. Paste your YAML, get JSON instantly — or paste JSON and convert to YAML. It validates both formats in real time and highlights errors with line numbers.
Common Pitfalls When Converting
There are several YAML features that do not have direct JSON equivalents. Being aware of them prevents surprises:
- Comments are lost. YAML supports comments with
#, but JSON does not. Any comments in your YAML will be stripped during conversion. - Anchors and aliases are resolved. YAML's
&anchor and*alias feature lets you reuse values. When converting to JSON, these are expanded into duplicated values. - Multi-line strings change format. YAML supports block scalars with
|(literal) and>(folded) indicators. In JSON, these become regular strings with\nescape characters. - Type coercion surprises. YAML auto-detects types. The value
onbecomes booleantrue,3.14becomes a number, andnullbecomes null. If you want the literal string "null", wrap it in quotes:"null". - Key ordering may change. YAML mappings are technically unordered, so some parsers may reorder keys when converting to JSON. Use an order-preserving parser if key order matters.
# YAML type coercion examples
truthy: yes # Becomes boolean true in JSON
falsy: no # Becomes boolean false in JSON
empty: null # Becomes null in JSON
version: 1.0 # Becomes number 1 (not 1.0!) in some parsers
octal: 0o777 # Becomes 511 in JSON
date: 2026-02-10 # Some parsers convert to a Date object
# To preserve as strings, use quotes:
truthy: "yes"
version: "1.0"
date: "2026-02-10"