Basics of YAML

Internship at OpenGenus

Get this book -> Problems on Array: For Interviews and Competitive Programming

In this article, we are going to learn the basics of YAML and try to explore it in deep. YAML is a serialization language which is used in configuration files and in transferring messages between different applications. So let's get started.

Table of contents:

  1. Introduction to YAML
  2. Sample YAML file
  3. Comments in YAML
  4. Numeric datatypes in YAML
  5. Strings in YAML
  6. Key:value pairs in YAML
  7. Boolean in YAML
  8. Nulls in YAML
  9. Arrays in YAML

Introduction to YAML

YAML is a serialization markup language which gains a lot of popularity and widely spread since last few years. It is used as a format for configuration files. It can act as a perfect replacement of a JSON(Javascript object notation) file as it has the object serialization attributes and due to this ability it can be a replacement of a JSON file. YAML can maps into data structures as it contains a wide range of language support. YAML Ain't Markup Language is the abbreviation of YAML.

Sample YAML file

Now after having a small basic knowledge of YAML we will learn about how a YAML file looks like. We will see some how a YAML looks like and learn each and everything about it.

  1. The file must start with three dashes. Here dashes represent the starting of a new YAML document as it supports various documents so when we put three dashes it will this as a new document.
  2. After that we have the most important part in any YAML file and that is key value pair. Most of you have heard this word as key value pair when you are learning python language. In python we have dictionary and in we create dictionary by using key value pair.
  3. The key:value pair can be of any data type like it can integer,strings,boolean or array.
  4. We can also use nested key value pairs but for that we have to use proper indentations or whitespace to specify that.

So now we have seen what are the basic elements in a YAML file so let's look at a YAML file. This will clear all your doubts about the points we have discussed above.

 opengenus: "It is good"
 site: "Opengenus"
 number: 8
 flag: true 
   fir: 7
   sec: "Hi"

Now this is a basic YAML file. We can see that it starts with three dashes. Then we can some key value pairs and that too with different data types as opengenus and site key are strings, then number key is integer, flag key is of boolean type, then we have an array which contains three elements with some whitespace as it shows nesting as they are part of array, at last we have a dict which can be viewed as a dictionary as it contains two key:value pairs. So this is the way of creating a YAML file and these are some basic elements or components in a YAML file. Let us dive into some more things which we can explore in YAML.

Whitespace or indentations are somehow play a vital role in YAML. When we do not specify any whitespace it will automatically treat that particular line as new line. As we have seen above in the file inside array we have used two whitespace as it indicates the nesting of elements inside array.

Comments in YAML

Now let's talk about how to add comments in a YAML file. Those who don't know what are comments let me first tell you about it. Comments are some lines which you can specify somewhere in your program or file to make it readable and understandable for any other person and for you as well when you revisit that program you can easily memorize what have you done here. So in a YAML file we can also add comments by simply adding # symbol and then write what you want to write. The important thing here is YAML processor will not gonna execute the comments as they are only for the person who is adding that or any other person.

This is how we can add comments in a file.

#This is a comment
#this is another comment

There are a lot of datatypes in YAML. We will see each one of them seperately which will definitely give you a clear and proper understanding.

Numeric datatypes in YAML

YAML support various types of numeric datatypes like integer,floating point,decimal,hexadecimal and octal. The file which we have seen above contains integers. In the same way there can be floating point numbers,decimal numbers,hexadecimal numbers and octal numbers. It also supports exponential floating point numbers. In this we can also use infinity and NAN( not a number ). Let us see with an example.

 a: 52
 b: 12d4
 c: 03323
 d: 4.2768
 e: 14.78e+07  #represent exponential floating point number
 f: .inf  #represent infinity
 g: .NAN  #represent not a number

now in this we can see that there are all numeric datatypes included.

Strings in YAML

In YAML we can use strings either with doublequotes or without any quotes. It will access that string without quotes also. But when we want to specify some operations like we want a line space between two different strings at that time we have to use doublequotes. Let's see an example.

 str: "Hello\n"
 st: Hello\n

When this is executed in a python code we will get the output code as:


This is due to quotes as after using quotes we YAML processor know that \n is used for next line but without using quotes it take the whole string considering \n also as a part of the string.

The another thing in strings is that if we want to print any value of a key which is a string in the same way as we what we have entered then it can be done using a vertical dash character(|). Example:

 who are you?

This will print the text in same line as:

hi who are you?

But using | :

a: |
 who are you?

And it will print:

who are you?

Key:value pairs in YAML

In YAML file key value pairs are very important. We have seen in the above YAML file that key value pairs can have different datatypes. The important thing here is that key should be always be a string as we have seen above it cannot be a number or anything else. Value can be of any datatype. Example:

a: "b"
c: 12

Keys are strings while value can be of any datatype.

Boolean in YAML

In YAML we can use boolean datatypes and for true condition we use True,On or Yes and for false condition we use False,Off or No. Example:

a: True
b: False
c: On
d: Off
e: Yes
f: No

All are correct and they are representing boolean values.

Nulls in YAML

In YAML nulls are also used for indicating nothing. They can be specified either by writing null or using ~ symbol. Example:

a: null
b: ~

In python it will print as:

a: None
b: None

Arrays in YAML

YAML supports usage of arrays in the file for storing a certain number of elements. We have seen in the file that how we use an array in a YAML file. There are different types of declarations how we can declare an array.

arr: [5,6,3,9]

We can also use nested representations also. The above two are the basic representations of an array in a YAML file.

So now we have seen some basic concepts and things which are part of YAML Ain't Markup Language. YAML is a very strong and powerful serialization language which is mostly used in configuration files and in transferring messages between different applications. Now I would like to conclude this article with a hope that you all will definitely read this article and try to learn by taking help from this article.

Happy learning.
Thank you.