Why Learn YAML?
- Human-Readable: Easier to write and understand than JSON and XML.
- Lightweight: Simple structure, indentation-based.
- Widely Used: Found in Kubernetes, CI/CD pipelines, Infrastructure as Code (IaC), API definitions, and more.
- Flexible: Supports hierarchical data, lists, and mappings efficiently.
Where is YAML Used?
YAML is used in various domains, including:- Cloud Infrastructure: AWS CloudFormation, Terraform.
- DevOps & CI/CD: GitHub Actions, GitLab CI/CD, Jenkins.
- API Definitions: OpenAPI (Swagger).
- Container Orchestration: Kubernetes.
- Static Site Generators: Jekyll, Hugo.
YAML vs JSON: Side-by-Side Comparison
Feature | YAML | JSON |
---|---|---|
Syntax | Uses indentation | Uses brackets {} and [] |
Readability | More human-friendly | Machine-friendly, more rigid |
Comments | Supports # for comments | No built-in comments |
Data Types | Strings, numbers, booleans, lists, maps, null | Same as YAML |
Complexity | Easier for configuration files | Better for data interchange |
File Size | Slightly larger due to formatting | Compact, no unnecessary spaces |
Basic Example
YAML
JSON
Getting Started with YAML
Basic Syntax
- Key-Value Pairs: Each key is separated by a colon.
- Indentation: Spaces (not tabs) define hierarchy.
- Lists: Represented using a hyphen (
-
).
Data Types in YAML
Real-World YAML Examples (Explained Line-by-Line)
1. Configuration Files
Example: Node.js App Configuration2. CI/CD Pipeline Configuration (GitHub Actions)
3. Kubernetes Deployment
Advanced YAML Features (Explained Line-by-Line)
1. Anchors & Aliases (Reusing Data)
2. Merging Multiple Files
3. Environment Variables in YAML
YAML Best Practices & Guidelines
Best Practices
- Follow Consistent Indentation (2 spaces recommended)
- Use Descriptive Keys
- Leverage Anchors for Reusability
- Use Comments for Readability
Common Mistakes to Avoid
- Using Tabs Instead of Spaces
- Incorrect Indentation
- Mixing Data Types in Lists