Basics of YAML

ยท

1 min read

YAML (YAML Ain't Markup Language) is a human-readable data serialization format used for configuration files, data exchange, and application setup.

Basic Data Structures

Scalars (Simple Values)

# Strings
name: John Doe
message: 'Hello, World!'

# Numbers
age: 30
pi: 3.14159

# Booleans
is_active: true
has_permission: false

# Null values
empty_value: null

Lists (Arrays)

# Simple list
fruits:
  - apple
  - banana
  - orange

# Mixed-type list
mixed_list:
  - 1
  - "string"
  - true

Dictionaries (Mappings)

person:
  name: Alice
  age: 25
  address:
    street: 123 Main St
    city: Wonderland

Nested Structures

company:
  name: Tech Corp
  employees:
    - name: John
      role: Developer
    - name: Jane
      role: Designer

Advanced Features

Anchors and References

defaults: &defaults
  adapter:  postgres
  host:     localhost

development:
  database: myapp_development
  <<: *defaults

production:
  database: myapp_production
  <<: *defaults

Multiline Strings

# Literal Block
description: |
  This is a multiline
  string that preserves
  line breaks

# Folded Block
folded_text: >
  This text will be
  folded into a single
  line with spaces

Type Casting

# Explicit Type Casting
age: !!int 30
pi: !!float 3.14
is_valid: !!bool true

Best Practices

  1. Use 2-space indentation

  2. Avoid tabs

  3. Use meaningful keys

  4. Keep it readable

  5. Validate YAML syntax

ย