Overview of YAML

YAML stands for Yet Another Markup Language or YAML ain't Markup Language.

All valid JSON files are valid YAML files.

YAML consists of two data blocks.

  1. Sequence, which is similar to a Python list. It begins with a dash("-") and a space.

     - orange
     - apple
     - banana
    
  2. Mapping, which is similar to a Python dictionary. It is a key-value pair.

     name: john
     age: 40
    

YAML also supports simple datatypes like number, strings, and boolean.

We can create lists of dictionaries, dictionaries containing lists and more.

  1. List of Dictionaries:

     - name: John
       age: 30
     - name: Cena
       age: 40
    

    In the above code snippet, we have a sequence with two items. Each item contains two mappings.

  2. Dictionary of lists:

     name:
       - John
       - Cena
       - Carter
    

    Here, we have a single mapping with the key "name" and the value is a sequence containing three items.

Important Note:

  1. If the YAML document starts with a mapping, it can contain only a series of mappings.

     name: John
     - 30 ## Invalid
    
  2. The same holds for the sequence as well.

We could nest them and create more complex documents.

- John :
    age: 50
    sex:
      - Male
      - Female
- Cena
- Carte
[
    {
        "John": {
            "age": 50,
            "sex": [
                "Male",
                "Female"
            ]
        }
    },
    "Cena",
    "Carte"
]