YAML: A short and crisp tutorial
Published: 2022-08-20 19:15:35

Before going into the details of YAML, what do you think YAML could be? One of the guesses by some of my friends was “You Ate My Lunch”. Jokes apart, let’s get started with the article.

Previously, YAML’s full form was “Yet Another Markup Language”. But now, it is “YAML Ain’t Markup Language”. By the way, what is a Markup language? 
One of the common examples of a markup language is HTML. But why is it a markup language? 

A markup language provides a structure to the page. Like, in HTML, you can give a definite structure to the page by providing, say, a header, under that header you can provide lists, and under the lists, you provide paragraphs and so on.

One question you would have in your mind is, “why YAML ain’t a markup language?”. First of all, YAML isn’t a programming language. It is a data format, used to exchange data, similar to XML and JSON data types. So, it is used to store information related to some configurations. Or simply put, you can’t write commands using YAML, you can only store data (this storing of data in files is known as data serialization).

**_Ok, so what are Data serialization and Deserialization?_**

Let’s take a hypothetical scenario:

Let’s say, the police department of Mumbai has a list of all the most wanted criminals in Mumbai. One of the criminals is say, Abdul.  The news comes out that Abdul has escaped Mumbai. The cops suspect that he might have run to Delhi, and thus they want to provide all details related to Abdul to the Delhi Police.  Let’s take that our police use web apps and machine learning models to transfer data to each other, predict a crime’s outcomes, or guess where the criminal might have gone. 

So, the problem at hand is, that data from the Mumbai police needs to be sent to a web app and further it needs to be sent to a machine learning model for carrying out the mission. So how is this data transfer done? Say, you have a file containing the data, how will you convert the file into a web app readable file and a machine learning model readable file? This process of file conversion is known as data serialization.

So, the process of converting data objects in complex data structures into streams of data to provide it to some devices is known as data serialization. The opposite of this is known as data deserialization.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/45vtq12k90zh1u4y4dks.png)

The files that store this object is known as data serialization file, and the language used is known as data serialization language. Examples of data serialization languages include YAML, JSON, and XML.  This file can now be shared anywhere easily, with no conversion at every step required.

An example-the Kubernetes way:

Imagine you have made an app and you want to deploy it on ten servers. For that, you have to provide data to Kubernetes about your file so that it can create pods in the servers according to your data. This data is given to Kubernetes by creating what is known as Kubernetes configuration files. These files are written in YAML.


 What are the benefits of YAML?

Some of the benefits include:
- Simple and easy to read
- Has a strict syntax, indentation is important
- Easily convertible to JSON, YAML, etc
- Most languages use YAML
- More powerful when representing complex data.
- Various tools like parsers can be used using YAML

**Background done, now let’s get started with some YAML coding:**

Some things to keep in mind:

-Extension for YAML files: .yaml or .yml.
-Note: YAML is case-sensitive. So “small” and “Small” are two different things.
-strictly following spaces and block style is important.
- to separate different types of documents (like lists, key-value pairs, etc), put “---” at the end of each document.
-for ending a document, put “…” at the end of the document.

As mentioned earlier, JSON, XML, and YAML are quite similar in function. And you might have to convert code from JSON/XML to YAML and vice versa or any combination of the three. Here’s a cool which might come in handy to you for code conversion: [Online YAML Tool](https://onlineyamltools.com/convert-yaml-to-json) .

A sample code:


```
# storing key value pairs(these are called maps):
"apple" : "red fruit"
2: "my roll number"
---
# lists 
- apple
- mango
- banana
- Apple
---
cities :
- wekm
- wekdw
- wedo
... #for ending the doc

#for storing lists in a single line:
cities: [wefwf,efe,efqeq2]
---

#for key value pairs in a single line:

{apple : "red fruit" , roll number : 58} 
---

``` 

***Datatypes in YAML- The basics:***

-everything after “:” is known as variable 


```
name: Yash #here Yash is a variable
``` 

Variables can be of many types like string, integer, etc :

**String:**


```
#string variables:
name: Yash #here Yash is a variable
address: "avgfwge"
gender: 'male'

``` 

**Note1: strings can be represented using no quotes/single quotes (‘ ‘ )/double quotes (“ ”)**

**Note2: say want to write multiple paragraphs/multiple lines of string. For that, you need to use “|” to preserve the format of your paragraph.**

Example: 


```
para: |
 the output
 will be in
 multiple lines.

``` 


**Note3: if your input is in multiple lines and you want to store it in a single line in the key, use “> “**

Example:

```
input: >
 this will
 be in
 a single line.

```

**Integer:**

```
#Integer data type:
number: 5461

```
**Float:**

```
#Float data type:
percentage: 96.4
``` 

**Boolean:**


```
#Boolean:
booleanValue: No # or you can also write n/N/false/False/FALSE
#or
BooleanValue: Yes # or you can also write y/Y/true/True/TRUE

``` 

**### Specifying the datatypes:**

Format:
key: !!datatype value 

Example:


```
#specifying the data type
number: !!int 1
hexa: !!int 0x45
floatingnumber: !!float 5464.168
infinity: !!float .inf
not a number: .nan
string: !!str this is a string
something: !!null Null #or null NULL ~
~: when your key is null
date: !!timestamp 2022-08-20
date with time: 2022-08-20T17:13:43.10 +5:30 # 17:13:43.10 is the UTC time by default, hence add 5:30 for IST
if no time zone: 2022-08-20T17:13:43.10
exponential number: 54851.648E161


``` 

**Now that we have learned the basic datatypes, let’s level up and learn some advanced ones:**


```
#sequence data type:
cities: [wefwf,efe,efqeq2] #can also be written as:
---
Cities: !!seq
 - efwef
 - wefwe
---
#sparse sequence: when some variable of the sequence is empty
 sparse sequence:
  -wfwf
  -wefwef
  -wefweefw
  - 
  -wefwewefwef   
---
#nested sequence:

- 
 -one
 -two
 -three
-
 -somthing
 -somthingmore
 -somethingonemore
---
#for maps, use !!map

#nested maps:
name: Yash
role:
 age: 58
 job: student

#this can also be represented as:
Name: Yash
Role: { age: 58, job: student}

#note: in the older version of Yakey-value pairs could have multiple values, but it’s not allowed:
#pairs example: !!pairs
# -nickname: Rahul
# -nickname: Raj
#the opposite of this is set datatype

#Dictionary datatype:
people: !!omap 
 - Sahil:
     name: Sahil P
     age: 54
     height: 468
 - Supra: 
     name: Supra P
     age: 57
     height: 548
#Anchor tags: used for avoiding repetition . example:
roles: &role
 role1: student
 role2: software engineer

 #say 3 people have the same roles as above. so instead of writing the same code again 3 times, we can use " <<: *role "" :

person1:
 name: yash 
 <<: *role

person2:
 name: supra
 <<: *role

person3:
 name: sahil 
 <<: *role

#say sahil has changed his role from software engineer to electronics engineer. we need to update that in code. for that we can do :

#person3:
# name: sahil
# <<: *role
# role2: electronics engineer
#writing role2 below <<: *role will override the data for role2

``` 


**Some handy tools to install:**
(Suggestion: Watch tutorials on Youtube on how to use this tools effectively. For setup and installation, follow the blogs attached below) 

 [Datree](https://hub.datree.io/)

[Monokle](https://kubeshop.github.io/monokle/)

[Lens](https://k8slens.dev/?utm_source=CloudNativeHackathon&utm_medium=Youtube&utm_campaign=DevOpsBoot)

Do check out [Tutorialspoint]( https://www.tutorialspoint.com/yaml/index.htm) for more info on YAML.


 **Outro:**

There’s no better way to test-drive YAML than by diving in and playing with it. The purpose of the blog is to create awareness about YAML. To learn further it is recommended to go through [YAML's blog](https://yaml.org/) and [GitHub](https://github.com/yaml/www.yaml.org/).
Have fun!
Feel free to connect with me on 
[LinkedIn](https://www.linkedin.com/in/yash-hegde-927721201/)
[Twitter](https://twitter.com/YashHegde7)
[GitHub](https://github.com/Yh010)

I would recommend you to go through this [article](https://spacelift.io/blog/yaml) as well for gaining more perspectives on this topic!

All code can be found on : [YAML Code](https://github.com/Yh010/YAML)
 Enjoy!