What is YAML?
YAML (YAML Ain’t Markup Language) is a human-readable data serialization format used for configuration files, data exchange, and automation scripts. It is widely used because of its clean syntax, readability, and simplicity.
YAML is commonly used in modern development tools like Docker, Kubernetes, and GitHub Actions.
Why Use YAML?
- Easy to read and write
- Less syntax compared to JSON/XML
- Supports hierarchical data
- Widely used in DevOps and cloud configurations
Basic YAML Syntax
YAML uses indentation (spaces, not tabs) to define structure.
Example 1: Simple Key-Value Pair
name: John Doe
age: 30
isDeveloper: true
Example 2: Nested Data
person:
name: John
age: 30
skills:
- C#
- JavaScript
- SQL
Example 3: List (Array)
fruits:
- Apple
- Mango
- Banana
YAML vs JSON
| Feature | YAML | JSON |
|---|---|---|
| Readability | High | Medium |
| Syntax | Simple | Strict |
| Comments | Supported | Not supported |
| Use Case | Config files | Data exchange |
Real-World Use Cases
1. Docker Configuration
Used in docker-compose.yml files:
version: '3'
services:
web:
image: nginx
ports:
- "80:80"
2. Kubernetes Deployment
Example of a deployment file:
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-app
spec:
replicas: 2
3. GitHub Actions (CI/CD)
name: Build Project
on: [push]
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2
Important Rules in YAML
- Use spaces for indentation (no tabs)
- Indentation defines structure
- Keys are case-sensitive
- Use
-for lists - Use
:to separate key and value
Common Mistakes
- Mixing tabs and spaces
- Incorrect indentation
- Missing colon
: - Improper list formatting
Advantages of YAML
- Clean and readable format
- Ideal for configuration files
- Supports complex data structures
Disadvantages of YAML
- Sensitive to indentation errors
- Parsing can be slower than JSON
- Not ideal for very large datasets
Conclusion
YAML is a powerful and flexible format that simplifies configuration management in modern development workflows. Whether you're working with Docker, Kubernetes, or CI/CD pipelines, learning YAML can significantly improve your productivity.
Bonus Tip
If you're a .NET developer, you can use libraries like:
- YamlDotNet for parsing YAML in C#
- Integrate YAML configs into ASP.NET MVC apps