Utility Coder
← Back to Blog
Configuration11 min read

YAML Complete Guide: Configuration and Data Serialization

Master YAML syntax, validation, and conversion. Learn YAML for config files, CI/CD, Kubernetes, and more.

By Andy Pham

YAML Complete Guide: Configuration and Data Serialization

YAML (YAML Ain't Markup Language) is a human-friendly data serialization standard used for configuration files, data exchange, and more.

YAML vs JSON

Feature YAML JSON
Comments Yes No
Readability Excellent Good
Complexity Higher Lower
Whitespace Significant Ignored
Multi-line strings Native Escaped

Basic YAML Syntax

Scalars

# Strings
name: John Doe
quoted: "Hello
World"
literal: |
  This is a
  multi-line string
folded: >
  This becomes
  a single line

# Numbers
integer: 42
float: 3.14
hex: 0xFF
octal: 0o755
scientific: 1.2e+10

# Booleans
enabled: true
disabled: false
on: yes
off: no

# Null
empty: null
also_empty: ~

Collections

# Lists
fruits:
  - Apple
  - Banana
  - Orange

# Inline list
colors: [red, green, blue]

# Maps
person:
  name: John
  age: 30
  city: NYC

# Inline map
point: {x: 10, y: 20}

# Nested
users:
  - name: John
    age: 30
    hobbies:
      - reading
      - coding
  - name: Jane
    age: 25

Advanced Features

# Anchors and aliases
defaults: &defaults
  timeout: 30
  retries: 3

production:
  <<: *defaults
  timeout: 60

development:
  <<: *defaults
  debug: true

# Multi-document
---
document: 1
---
document: 2

YAML to JSON Conversion

# YAML
name: John
age: 30
skills:
  - JavaScript
  - Python
{
  "name": "John",
  "age": 30,
  "skills": ["JavaScript", "Python"]
}

JavaScript Conversion

const yaml = require('js-yaml');

// YAML to JSON
const data = yaml.load(yamlString);
const json = JSON.stringify(data, null, 2);

// JSON to YAML
const yamlOutput = yaml.dump(JSON.parse(jsonString));

Python Conversion

import yaml
import json

# YAML to JSON
with open('config.yaml') as f:
    data = yaml.safe_load(f)
json_str = json.dumps(data, indent=2)

# JSON to YAML
yaml_str = yaml.dump(json.loads(json_str))

Common Use Cases

Docker Compose

version: '3'
services:
  web:
    image: nginx
    ports:
      - "80:80"
  db:
    image: postgres
    environment:
      POSTGRES_PASSWORD: secret

GitHub Actions

name: CI
on: [push, pull_request]
jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - run: npm install
      - run: npm test

Kubernetes

apiVersion: v1
kind: Pod
metadata:
  name: nginx
spec:
  containers:
    - name: nginx
      image: nginx:latest
      ports:
        - containerPort: 80

YAML Validation

Common errors:

  • Incorrect indentation (must use spaces, not tabs)
  • Missing colons after keys
  • Improper quoting
  • Duplicate keys

Best Practices

  • Use 2-space indentation
  • Quote strings with special characters
  • Use anchors for repeated values
  • Validate before deploying
  • Keep files simple

Try Our YAML Tools

Conclusion

YAML's human-readable syntax makes it ideal for configuration. Understanding YAML is essential for DevOps, Kubernetes, and modern development workflows.

Share this article