Everything You Need To Know About YAML

Mohsin Zaheer Mohammed
5 min readMay 2, 2020

--

Beginner Guide To YAML

Overview

YAML is a human readable data serialization language mainly used for configuration files and in application in order to store or transmitted data. It is widely used by many programming languages like Java, Python, GoLang and as a configuration language by Kubernetes, Docker Compose, Ansible, etc.

What should I used “.yaml” or “.yml”?

YAML file is created with an extension of “.yaml” or “.yml”. You can use any one format of extension as both works exactly same. I personally like “.yml” as it looks cool than “.yaml”

Basic Structure

YAML basic structure is key and value pairs. Key is always a string whereas value can be any data type. Data type supported by YAML are

  1. String
  2. Integer
  3. Boolean
  4. Array
  5. Dictionaries
YAML Config File

You will be saying, what the heck “ — -” is? That actually mean YAML file is starting from there. In order to comment something then simple use “#” in front of a line. In the above image, “title” is key and “YAML Ain’t Markup Language” is the value whereas “learn” is another key with a list.

Value as List in YAML

YAML support List/Array as a data type. List can be individual list or complex list. Let's first see how to define individual list. You can define it as in line style between square brackets separated by commas or passing on individual lines with “-” in front of it. I prefer passing it on individual lines with “-” as it looks cleaner and easier to read.

In Line Style List

In Line List

In the above image, hobbies are defined as in line style list.

Individual Line Style List

Individual Line List

In the above image, hobbies are defined as an individual line style list.

Complex List

Complex List

Till now we have seen a list which contains individual items but what if a list also has another list of items. YAML support complex list as well and if you see above image, a person’s key is having a list of person’s and each person’s list is having another list containing details of each person. In the above image, there are three person and each list containing their first name, last name and age.

Formatting in YAML

It’s fine if the value is small but what if the value is a long string or you want to use a small script in your configuration file? If you simple use multiline value, then YAML won’t be able to read the configuration and throw an error but you can use a multiline value by using “>” and “|”.

Using “>” for multiline value

Using “>” for multiline value

If you see the above image, we are using “>” for multiline value and what this is going to do is wherever you hit enter for multiline is basically going to replace with a space and when you call this key it’s going to return the value as a long single string. If you want to have new line at the end of the string, then use “>+” or if you have a new line and you want to remove it then use “>-”

Using “|” for multiline value

Using “|” for multiline value

We have seen how to use multiline value but what if you want to preserve the format and want each in new line. Then you can use “|” instead of “>” and it will preserve the format. Again, if you want to add new line at the end then use “|+” or to remove new line then use “|-”

Anchor

You would have heard from lot of people saying don’t repeat yourself. What if you have a value in your YAML file and want to use it at another place in your file. Anchor can help you with getting those value without repeating yourself.

Anchor Individual Value

Basically, you have define an anchor for the key you want to use by using “&anchorname” personally I like to keep the anchor name same as the key name so that I can easily understand what value I’m using it but you can keep any name then you can call that value in your YAML file by using “*anchorname” and YAML will replace with the value. In the above image, I have created an anchor of name and use that value for id

Anchoring Key Value Pair

You can even render key: value by using anchor. If you see above image, I create a key value pair. In order to render key value, you need to use “<<: *anchorname” then it will basically be going to replace that line with that key value pair.

Change Data Type

Changing Data Type

By default, YAML renders the data type of the value but if you want to change the data type then you can do it using “!!float 27” or “!!str 3.5”.

--

--