Wednesday, May 13, 2026

A quick reference to YAML

YAML is designed to be human-readable and maps directly to native data types like lists, dictionaries, and strings. Here is a brief summary of the essential syntax:


1. Basic Structures

  • Key-Value Pairs: Created with a colon and a space.

    key: value

  • Comments: Begin with a hash symbol.

    # This is a comment

  • Document Separators: Use --- to start a new document and ... to end one (optional).


2. Collections (Arrays and Objects)

  • Lists (Sequences): Use a dash followed by a space for each item.

    YAML
    - Item 1
    - Item 2
    
  • Dictionaries (Mappings): Nested using indentation (usually 2 spaces).

    YAML
    parent:
      child1: value
      child2: value
    

3. Multiline Strings (Scalars)

  • Literal Block (|): Preserves newlines and trailing whitespace.

  • Folded Block (>): Folds newlines into spaces (converts a block into a single paragraph).


4. Data Types

YAML automatically detects types, but you can explicitly cast them using !!.

  • Strings: Can be unquoted, single-quoted (literal), or double-quoted (supports escape sequences like \n).

  • Numbers: Integers (12) and Floats (3.14).

  • Booleans: true, false, yes, no.

  • Nulls: null or ~.


5. Advanced Features

  • Anchors (&) and Aliases (*): Used to duplicate or reference data to avoid repetition.

    YAML
    default: &base
      adapter: postgres
    development:
      <<: *base
    
  • Complex Keys: Use ? for keys that are themselves objects or lists.

  • "<<" : is the merge operator, which indicates the section to be replaced with the alias indicated by "*" after "<<". 


Quick Reference Table

FeatureSyntax ExampleNotes
Mappingname: GeminiRequires space after :
List- appleRequires space after -
Inline List[1, 2, 3]JSON-style syntax
Inline Map{a: 1, b: 2}JSON-style syntax
Literal String``
Folded String>Newlines become spaces

No comments:

Post a Comment

CrewAI "Hello World" in a single colab sheet, without YAML files

 [Cell 001] ! pip install -U crewai [Cell 002] from google.colab import userdata import os os.environ[ "GOOGLE_API_KEY" ] = use...