DEV Community

Cover image for YAML: A short and crisp tutorial
Yash Hegde
Yash Hegde

Posted on • Edited on

YAML: A short and crisp tutorial

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

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 .

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} 
---

Enter fullscreen mode Exit fullscreen mode

Datatypes in YAML- The basics:

-everything after “:” is known as variable

name: Yash #here Yash is a variable
Enter fullscreen mode Exit fullscreen mode

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

String:

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

Enter fullscreen mode Exit fullscreen mode

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.

Enter fullscreen mode Exit fullscreen mode

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.

Enter fullscreen mode Exit fullscreen mode

Integer:

#Integer data type:
number: 5461

Enter fullscreen mode Exit fullscreen mode

Float:

#Float data type:
percentage: 96.4
Enter fullscreen mode Exit fullscreen mode

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

Enter fullscreen mode Exit fullscreen mode

### 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


Enter fullscreen mode Exit fullscreen mode

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

Enter fullscreen mode Exit fullscreen mode

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

Monokle

Lens

Do check out Tutorialspoint 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 and GitHub.
Have fun!
Feel free to connect with me on
LinkedIn
Twitter
GitHub

I would recommend you to go through this article as well for gaining more perspectives on this topic!

All code can be found on : YAML Code
Enjoy!

Top comments (0)