YAML 101: A Beginner's Guide

YAML (short for YAML Ain't Markup Language - ignore the tautology, everyone does), is a data serialization language commonly used for configuration file and data interchange between programming languages. This simply means that it can be used to share data among apps with different data structures.

This along with it's human-readable format/syntax means that it is the most commonly used configuration file format ahead of other serialization languages such as JSON and XML.

It should be noted however that YAML is a superset of JSON, which means that any valid JSON file (.json) is also a valid YAML file (.yaml, .yml) and vice versa (with some exceptions).

SYNTAX

  1. Data Types

    YAML supports several data types. these include:

    • Scalars: These are single values, such as strings, numbers, and booleans.

    • Lists: Ordered sequences of items.

    • Maps: Unordered key-value pairs.

  2. Indentation

    YAML uses indentation (typically spaces, not tabs) to indicate structure. The number of spaces used for indentation is not strictly defined, but it's important to be consistent as the file wouldn't be read otherwise.

    For such reason, it is important to validate every YAML file before running it either via plugins in your code editor or online tools.

  3. Comments

    Comments in YAML start with the # character and continue until the end of the line. Although, they are ignored by parsers, this feature adds to the overall readability of YAML code.

  4. Scalars

    Scalars are simple values like strings, numbers, and booleans. They don't require any special syntax. For example:

     name: John Doe
     age: 30
     is_student: false
    
  5. Lists

    Lists are denoted by a hyphen (-) followed by space and items listed under it. For example:

     fruits:
       - apple
       - banana
       - orange
    
  6. Maps

    Maps are key-value pairs. They are represented using colon (:) to separate keys and values. For example:

     person:
       name: John Doe
       age: 30
       address:
         city: New York
         country: USA
    
  7. Nested Structures

    YAML allows nesting of lists and maps within each other to represent complex data structures. For example:

     users:
       - name: Alice
         age: 25
       - name: Bob
         age: 30
    
  8. Quoted Strings

    If a string contains special characters, it should be enclosed in quotes. YAML supports both single (') and double (") quotes. For example:

     message: "This is a 'quoted' string."
    
  9. Multi-line Strings

    Multi-line strings can be represented using the > or | indicators. > removes leading whitespace, while | preserves it. For example:

     description: >
       This is a long description
       that spans multiple lines.
    
  10. Multiple Components

    YAML also supports defining multiple components within a single YAML file. This is often done for organizational purposes or when different components are related to each other. Each component can be separated by a delimiter like --- (three hyphens) to indicate the start of a new document within the YAML file.

This feature is widely used in configuration files, in Infrastructure as code (IaC) and staples DevOps such as Docker and Kubernetes.

Here's an example of a YAML file with multiple components:

    # Component 1: Configuration Settings
    database:
      host: localhost
      port: 3306
      username: admin
      password: secret
    ---
    # Component 2: List of Users
    users:
      - name: Alice
        age: 25
      - name: Bob
        age: 30
    ---
    # Component 3: Additional Configuration
    server:
      host: example.com
      port: 8080
    ---
    # Component 4: List of Products
    products:
      - name: Laptop
        price: 1000
      - name: Smartphone
        price: 500

In this example:

  • All components are separated by ---, indicating the start of a new document.

  • Component 1 defines configuration settings for a database.

  • Component 2 contains a list of users.

  • Component 3 defines additional configuration settings for a server.

  • Component 4 contains a list of products.

When parsing such a YAML file, a YAML parser can read each component separately, treating them as distinct YAML documents within the same file. This allows for organizing related data or configuration settings in a single file while keeping them logically separated.


It can easily be seen from the examples above that YAML's syntax is both easily readable and even intuitive. As such, it is important to not try to memorize any of these as repeated practice will make them second nature and you can always check this article and other resources online for any help that might be needed.

With that said, if you enjoyed this article and are interested in learning SQL, you can check out SQL 101 here.