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.
Sequence, which is similar to a Python list. It begins with a dash("-") and a space.
- orange - apple - banana
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.
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.
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:
If the YAML document starts with a mapping, it can contain only a series of mappings.
name: John - 30 ## Invalid
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"
]