Sample of Yaml file & its structure explained

Yaml is a data structure format, It is necessary to understand this format as it is used widely in Kubernetes & docker. There are other data structure formats as well such as JSON & XML.

The simplest form of data you can note in Yaml is Key-Value Pairs.

Fruit: Apple
Vegetable: Carrot
Grain: Rice

Now let’s say you have to define an Array. The – represents element of an Array. Arrays are also known as Lists. An Array or List is generally used when we need to store multiple items of same type of Object.

Fruits:
     - Banana
     - Apple
     - Mango
Dry Fruits:
     - Dried Fig
     - Cashew nuts
     - Dates

Dictionary : A dictionary is a set of properties defined under an Item. Let’s say we want to write properties of a computer. You mush have equal number of black spaces before each item.

Lenovo Yoga:
    Ram: 16 GB
     CPU: 4 cores
     GPU: 2 GB
     Storage: 1TB
Macbook Pro:
     Ram : 16 GB
     CPU: 8 cores
     GPU: 1 GB
     Storage: 128 GB

When to use dictionary & when to use lists ? Generally Speaking, to store properties of a Single Object we use Dictionaries. For Example, let us say I have Car, Now I would like to list properties of my car such as its’s color, model etc… Then I would use a dictionary.

My Car:
    Model: TATA Nexon
     Model Year: 2024
     Color: Black
     Engine Variant : Petrol 1.2L 
     Transmission : Automatic

This need not be as simple as this, it can be split into a dictionary in another dictionary. For example, the same information above can also be represented as follows. A single Value of Model is split into 2 Key value pairs of Name & Year.

My Car:
    Model: 
        Name: Tata Nexon
        Year: 2024
     Color: Black
     Engine Variant : Petrol 1.2L 
     Transmission : Automatic

List of Dictionaries: Let us say, we have 2 cars & we want to list their properties. The below example is called List of Dictionaries.

    - Car1:
         Model:
             Name: Tata Nexon
             Year: 2024
         Color: Red
          Transmission : Manual
    - Car2:
         Model:
             Name: Tata Harrier
             Year: 2021
         Color: Blue
          Transmission : Auto

Note: The Order in Dictionary does not Matter, but the List are ordered collection, so the order of the items matter.

error: Content is protected !!