Understanding YAML: A Beginner-Friendly Guide

If you've ever worked with configuration files or data exchange in programming, you might have come across YAML. It’s a simple, human-readable way to store and share data. In this blog post, we’ll break down what YAML is, its features, where it’s used, and how it compares to other formats like JSON—all in an easy-to-understand way!

What is YAML?

YAML stands for "YAML Ain’t Markup Language". It’s a data serialization format, which means it helps convert complex data (like objects or lists) into a format that’s easy to store, transfer, and read later. Think of it like packing your data into a neat, readable package.

Unlike other formats that can look cluttered with brackets and commas, YAML is designed to be human-friendly. Its clean syntax makes it easy to write and understand, even for beginners.

What is Serialization?

Before we dive deeper, let’s quickly explain serialization. It’s the process of turning data (like a list or an object in a program) into a format that can be saved or shared. For example, if you have a list of names in your code, serialization converts it into something like a text file that you can send to another system. YAML is one of the tools that makes this process simple and readable.

A Brief History of YAML

YAML was first proposed by Clark Evans in 2001 to create a format that’s easier to read than XML. Here’s a quick timeline:

  • 2001: YAML was introduced.

  • 2004: The first production version, YAML 1.0, was released.

  • 2009: The current version, YAML 1.2, came out.

The goal was always to make a format that’s intuitive for humans while still being useful for machines.

Key Features of YAML

YAML has some standout features that make it popular:

  • Readable: It’s designed to be easy for humans to read and write.

  • Indentation-Based: Instead of using brackets or commas, YAML uses spaces (indentation) to show structure.

  • Supports Comments: You can add notes in your YAML file using the # symbol.

  • Key-Value Pairs: Data is organized as keys (like labels) and values (the data itself).

  • Flexible File Extensions: YAML files can end in .yaml or .yml—both work the same.

Where is YAML Used?

YAML is widely used in tools and platforms, especially for configuration files. Here are some popular examples:

  1. Kubernetes: Defines how applications run in containers.

  2. Docker Compose: Configures multi-container Docker applications.

  3. GitHub Actions/GitLab CI: Automates workflows for code testing and deployment.

  4. Ansible: Manages server configurations.

  5. AWS CloudFormation: Sets up cloud infrastructure.

  6. OpenAPI (Swagger): Describes APIs.

  7. Helm Charts: Manages Kubernetes packages.

  8. Prometheus: Configures monitoring systems.

If you’re working with modern DevOps or cloud tools, chances are you’ll encounter YAML.

YAML vs. JSON: What’s the Difference?

YAML is often compared to JSON, another popular data format. Let’s see how they stack up:

FeatureJSONYAML
SyntaxStrict, uses {} for objects and [] for arraysLooser, uses indentation and : for key-value pairs
ReadabilityLess human-friendly, more for machinesEasier to read and write for humans
CommentsNot supportedSupported with #
Multi-line StringsNot supported nativelySupported with `
File SizeSlightly larger due to brackets/quotesMore compact
Use CaseAPIs, web servicesConfiguration files, CI/CD pipelines

YAML shines in situations where readability matters, like configuration files, while JSON is often used for data exchange in APIs.

Features YAML Has That JSON Doesn’t

YAML has some unique features that JSON lacks:

  • Multi-line Strings: Write paragraphs without breaking the format.

  • Comments: Add explanations right in the file.

  • Anchors: Reuse data within the same file.

  • Merging: Combine data structures easily.

  • Type Casting: Explicitly define data types (like integers or strings).

YAML Data Structures

YAML supports a variety of data types, making it versatile. Here’s what you can work with:

Scalars (Single Values)

  • String: Text, like "John Doe" or 'simple'.

  • Number: Integers (95), floats (3.14159), hex (0x1A), or octal (0644).

  • Boolean: true or false.

  • Null: Represented as ~, null, or an empty value.

  • Date: ISO 8601 format, like 2024-12-22T10:30:00Z.

Sequences (Lists/Arrays)

  • Lists are written with dashes (-):

      skills:
        - Python
        - JavaScript
        - Docker
    
  • Inline lists use square brackets:

      hobbies: [Reading, Hiking, Chess]
    

Mappings (Dictionaries/Objects)

  • Key-value pairs are separated by colons (:):

      address:
        street: 123 Main St
        city: New York
        zip: 10001
    
  • Inline dictionaries use curly braces:

      database: { host: localhost, port: 5432 }
    

Writing YAML: Common Rules

Here are some simple rules to follow when writing YAML:

  • Indentation: Use 2 spaces (not tabs) to define structure.

  • Colons (:): Separate keys from values.

  • Dashes (-): Indicate list items.

  • Comments: Start with # for notes.

  • Quotes: Use single (') or double quotes (") when you want a number to be treated as a string (e.g., number: "20").

  • Null Values: Use ~, null, or leave the value empty (key:).

  • Multi-line Strings:

    • Use | to keep line breaks:

        bio: |
          John is a software engineer.
          He loves coding.
      
    • Use > to fold lines into a single line:

        summary: >
          John works at a tech company.
          He freelances part-time.
      

YAML Example

Here’s a sample YAML file showing different data types:

# Person details
name: John Doe
age: 30
is_active: true
# String with quotes
country: "United States"
# List
skills:
  - Python
  - JavaScript
  - Docker
# Inline list
hobbies: [Reading, Hiking, Chess]
# Dictionary
address:
  street: 123 Main St
  city: New York
  zip: 10001
# Multi-line string
bio: |
  John is a software engineer.
  He loves to write clean code.
# Date
birthday: 1993-07-15

This example is clean, readable, and shows how YAML handles different data types.

Advanced YAML Features

Anchors and Aliases

You can reuse data with anchors (&) and aliases (*):

defaults: &default_settings
  retries: 5
  timeout: 30
api_config:
  <<: *default_settings
  endpoint: /api/v1/data

This saves time by avoiding repetition.

Multi-Document YAML

You can include multiple YAML documents in one file, separated by ---:

# Document 1
apiVersion: apps/v1
kind: Deployment
---
# Document 2
apiVersion: v1
kind: Service

This is common in tools like Kubernetes.

Explicit Type Casting

You can force a specific data type using tags like !!int or !!float:

age: !!int "30"  # Treats "30" as an integer
price: !!float "19.99"  # Treats "19.99" as a float

Environment Variables

YAML can pull in environment variables dynamically:

database:
  url: ${DB_URL}
  username: ${DB_USER}
  port: ${DB_PORT:-5432}  # Default to 5432 if not set

YAML in Action: A Real-World Example

Imagine you’re setting up a Docker Compose file to run a web app. Here’s how it might look in YAML:

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

This file tells Docker to run an Nginx web server and a PostgreSQL database, with specific configurations. It’s easy to read and modify, even for non-experts.

Tips for Working with YAML

  1. Watch Indentation: Always use 2 spaces and avoid tabs to prevent errors.

  2. Validate Your YAML: Use online tools like YAML Lint to check for syntax errors.

  3. Keep It Simple: Avoid overly complex structures when possible.

  4. Be Cautious with Security: YAML can execute code in some parsers, so validate inputs if you’re accepting YAML from users.

Conclusion

YAML is a powerful yet simple format that’s perfect for configuration files and data exchange. Its human-readable syntax, support for comments, and flexibility make it a favorite in tools like Kubernetes, Docker, and CI/CD pipelines. Whether you’re a beginner or an experienced developer, learning YAML can make your work with configuration files much easier.

Ready to try YAML? Start by writing a simple configuration file for a project, and you’ll see how intuitive it is. If you have questions or want to share your YAML tips, let us know in the comments!

1
Subscribe to my newsletter

Read articles from Md.Rejoyan Islam directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Md.Rejoyan Islam
Md.Rejoyan Islam

I’m Md Rejoyan Islam, a full-stack web developer with a passion for creating impactful digital experiences. Proficient in JavaScript, Python, React.js, Next.js, Node.js, and both SQL and NoSQL databases. Always eager to learn and adapt to new technologies.