YAML Basic Syntax

Learn the fundamental building blocks of YAML syntax, including indentation rules, comments, and basic structure.

What is YAML?

YAML (YAML Ain't Markup Language) is a human-readable data serialization standard that is commonly used for configuration files, data exchange, and application settings. YAML is designed to be easy to read and write, making it an excellent choice for both humans and machines.

Key Features of YAML:

  • Human-readable: Easy to read and understand
  • Indentation-based: Uses spaces for structure
  • No brackets: Clean, minimal syntax
  • Cross-language: Works with many programming languages

Basic YAML Structure

YAML uses indentation to represent structure. The basic building blocks are key-value pairs, lists, and nested objects.

Simple Key-Value Pairs

name: John Doe
age: 30
email: john@example.com

Nested Objects

person:
  name: John Doe
  age: 30
  address:
    street: 123 Main St
    city: New York
    country: USA

Lists/Arrays

fruits:
  - apple
  - banana
  - orange

# Or inline
colors: [red, green, blue]

Indentation Rules

Indentation is crucial in YAML. It defines the structure and hierarchy of your data.

⚠️ Important Indentation Rules:

  • Use spaces for indentation (not tabs)
  • Be consistent with your indentation level
  • Common indentation: 2 spaces per level
  • Don't mix tabs and spaces

Correct Indentation

# ✅ Good - 2 spaces per level
database:
  host: localhost
  port: 5432
  credentials:
    username: admin
    password: secret

Incorrect Indentation

# ❌ Bad - inconsistent indentation
database:
    host: localhost
  port: 5432
    credentials:
      username: admin
        password: secret

Comments

Comments in YAML start with the # symbol and can be placed anywhere in the file.

Comment Examples

# This is a full-line comment
name: John Doe  # This is an inline comment
age: 30

# Multi-line comments
# are just multiple
# single-line comments

database:
  # Database configuration
  host: localhost
  port: 5432
  # Credentials are below
  username: admin
  password: secret

Strings

YAML supports different ways to write strings, each with its own use case.

String Types

# Simple strings (no quotes needed)
name: John Doe
age: 30

# Quoted strings (when needed)
description: "This is a string with special characters: @#$%"
title: 'Single quotes also work'

# Multiline strings
description: |
  This is a multiline string
  that preserves line breaks
  and formatting

# Folded strings (single line)
summary: >
  This is a folded string
  that will be converted
  to a single line

Numbers and Booleans

YAML automatically detects numbers and boolean values.

Numbers

# Integers
count: 42
negative: -10

# Floats
price: 19.99
percentage: 85.5

# Scientific notation
large_number: 1.23e+10

Booleans

# Boolean values
is_active: true
is_deleted: false

# Alternative boolean values
enabled: yes
disabled: no
on: on
off: off

Null Values

YAML represents null/empty values in several ways.

Null Value Examples

# Explicit null
value: null

# Tilde notation
empty: ~

# Empty value
missing:

# All represent the same thing

Common Mistakes to Avoid

⚠️ Common YAML Mistakes:

  • Inconsistent indentation: Mixing tabs and spaces
  • Missing colons: Forgetting : after keys
  • Wrong list syntax: Using [] instead of -
  • Unquoted special values: Not quoting strings that look like other types

Common Mistakes

# ❌ Wrong - missing colon
name John Doe

# ❌ Wrong - inconsistent indentation
person:
  name: John
    age: 30

# ❌ Wrong - using brackets for lists
fruits: [apple, banana]

# ✅ Correct
name: John Doe
person:
  name: John
  age: 30
fruits:
  - apple
  - banana

Practice Examples

Try these examples to practice your YAML skills:

Exercise 1: Basic Structure

Create a YAML file for a user profile with name, email, age, and a list of hobbies.

Show Solution
user:
  name: Jane Smith
  email: jane@example.com
  age: 28
  hobbies:
    - reading
    - hiking
    - photography

Exercise 2: Nested Configuration

Create a configuration file for a web application with database and server settings.

Show Solution
app:
  name: "My Web App"
  version: "1.0.0"
  debug: true

database:
  host: localhost
  port: 5432
  name: myapp
  credentials:
    username: admin
    password: secret123

server:
  host: 0.0.0.0
  port: 8080
  ssl: false