Introduction

In the next 2 following articles, we will see together how YAML Files are created to store data and how we can retrieve data from them.

It will be useful in a lot of my incoming articles (i.e Kubernetes series (!))

YAML (“YAML Ain’t Markup Language”) is a human-readable data-serialization language.

It is less complex and ungainly than XML or JSON, but provides similar capabilities. It essentially allows you to provide powerful configuration settings, without having to learn a more complex code type like CSS, JavaScript, and PHP.

Here are the following basic rules YAML has in place to avoid issues related to ambiguity in relation to various languages and editing programs :

  • YAML files should end in .yaml whenever possible in Grav.
  • YAML is case sensitive.
  • YAML does not allow the use of tabs. Spaces are used instead as tabs are not universally supported.

A cheat sheet and full specification are available at the official site : Here

Understand YAML Files

If you have already worked with XML or JSON, you will easily pick it up. If not, in my opinion the best way to understand how it works is through exercices.

A YAML file is basically used to store data. And even in YAML, you have multiple ways to store date.

How to store data in a YAML File

Key Value Pair

The first way to store data is to use a Key Value Pair like so :

Car: Audi
Fruit: Apple
Animal: Alligator

You can see that the keys (“Fruit”, “Car” and “Animal”) are separated by a colon to their values (“Audi”, “Apple”, “Alligator”). It’s important to have a space after the colon !

Arrays / Lists

Here is how you can store arrays in a YAML File :

Car: 
  - Alpha Romeo
  - Audi


Fruit: 
  - Apple
  - Apricot


Animal: 
  - Alligator
  - Alpaca

It’s pretty easy as you can see. Just notice that the dash means that the Element is an Array.

Dictionaries

A dictionary is a set of properties grouped together.

Car: 
  Brand: Audi
  Model: A3
  Color: Black
  

Here we try to represent all information we have on a specific car. Notice the blank space before each items. You must have equals number of blank spaces.

Mixed data

We can have multiple ways combined together to store our data. Like lists containing dictionnaries containing lists.

Here we have a list of cars and the elements of the lists are “Audi” and “BMW” but each of these elements are dictionnaries containing information on the specified car :

Cars:
  - Audi: 
      Owner: John Doe
      Model: A3
      Color: Black
  
  - BMW: 
      Owner: Jane Doe
      Model: A3
      Color: Black

When do we I need to use a dictionary or a list ?

First of all, it is important to understand that YAML files are used to store data. It could be data about anything.

If we continue to take as an example, “cars”, the best way to store informations on it is a dictionary. We can store all information we want (“owner”, “color”, “model”, “price” etc) :

Car: 
  Brand: Audi
  Model: A3
  Color: Black
  Owner: John Doe
  Price: $40,000

However if we want to split information like the owner into different parameters like the “familiy name”, the “age” or the “firstname”, dictionnaries won’t be enough. We will use a dictionary in our previous dictionary :

Car: 
  Brand: Audi
  Model: A3
  Color: Black
  Owner:
    FamilyName: Doe
    FirstName: John
    Age: 33
  Price: $40,000

Now, imagine that we want to store 3 cars, with 3 different owners. The best way to do that is to use a list of dictionaries like so :

- Car1:
    Brand: Audi
    Model: A3
    Color: Black
    Owner:
        FamilyName: Doe
        FirstName: John
        Age: 33
    Price: $40,000


- Car2:
    Brand: BMW
    Model: Z3
    Color: White
    Owner:
        FamilyName: Doe
        FirstName: Jane
        Age: 28
    Price: $50,000


- Car3:
    Brand: Tesla
    Model: Model 3
    Color: Blue
    Owner:
        FamilyName: Morgan
        FirstName: Dexter
        Age: 35
    Price: $60,000

Important Note

  • Dictionaries are unordered !
  • Lists or Arrays are ordered !

That’s all, I hope it will give you enough tips to be able to read or write YAML files easily.

In the next article, we will see together JSON files. It will be easy to understand as you know how to read YAML files.

We will also see together how we can retrieve data from it ! 🙂

Sources :

wikipedia

yaml.org

Leave a Comment

Your email address will not be published. Required fields are marked *