Isaac.

YAML Guide

Table of Contents

  • What is YAML?
  • The Absolute Basics: Syntax & Structure
  • Intermediate Concepts: Making it Powerful
  • Advanced Features: Reusability & Efficiency
  • Common Pitfalls and How to Avoid Them
  • YAML vs. JSON
  • Quick Start Guide

What is YAML?

YAML stands for YAML Ain't Markup Language (a recursive acronym). It started as "Yet Another Markup Language" but was redefined to distance itself from just being a markup language like XML.

  • Human-Readable: It's clean and easy to write and understand, much more so than JSON or XML.
  • Data-Oriented: It's designed to represent data structures (like lists and dictionaries) rather than documents.
  • Versatile: It can represent scalars (single values), lists, and associative arrays (dictionaries/hashes).

Common Use Cases:

  • Configuration files (Docker, Kubernetes, Ansible, GitHub Actions)
  • Data serialization (exchanging data between languages)
  • Property files

The Absolute Basics: Syntax & Structure

1. Key-Value Pairs (Maps/Dictionaries)

name: John Doe
age: 30
city: New York

2. Lists (Sequences)

fruits:
  - Apple
  - Banana
  - Orange
fruits: [Apple, Banana, Orange]

3. Nested Structures

person:
  name: John Doe
  age: 30
  hobbies:
    - hiking
    - reading
    - coding
  address:
    street: 123 Main St
    city: New York

YAML vs. JSON

FeatureYAMLJSON
ReadabilityHigh, less punctuationLower, more brackets and quotes
CommentsYesNo
Multi-line StringsEasy (with | or >)Difficult (requires \n)

Quick Start Guide

  1. Start Simple: Begin with key-value pairs.
  2. Indent with Spaces: Always use 2 or 4 spaces, never tabs.
  3. Use a Linter/Validator: Many online tools and code editor plugins (like YAML plugins for VS Code) can validate your YAML and catch errors before you run your code.
  4. Practice: Look at configuration files for tools like Docker Compose or GitHub Actions to see real-world examples.