YAML for Beginners: A Crash Course

Vishal PrakashVishal Prakash
6 min read

Table of contents

YAML - Yet Another Mark Up Language —> But Now Called —> YAML Ain’t Markup Language

MARKUP LANGUAGES ——>

a text-encoding system consisting of a set of symbols inserted in a text document to control its structure, formatting, or the relationship between its parts.

What goes inside what in HTML (This is where markup language is used).

YAML-

  • It is not a programming language. It is a Data Format that is used Exchange

  • It is similar to XML and JSON datatypes.

  • It is simple human-readable language that can be used to represent data. It is used to store some information about the configuration.

  • In YAML, we can store only data and not commands. —> This thing is known as storing the data in files called data Serialisation.

Data Serialisation

Objects(Code + Data): is nothing but a collection of data. —> For example in memory we have objects like related to - an object of a particular student and that has some type of data type like roll no, regno, marks

Serialisation is a process of converting the data objects into a complex Data Structure (a stream of bytes) that can be used to transfer the data on our physical devices.

Stream - Stream means chunks of data (Byte Stream).

Data Serialisation language - If we want to represent these objects in a file that we can read code or modify in those files is known as data serialisation language. —> The language which is used to represent this data in the text format. eg- YAML, JSON, XML

In this way, we can use these languages to represent data in code. So we can store data in the format of code. Then we can use that file to convert it into an object. This is known as deserialisation.

OBJECT to FILES —-> Serialisation

FILE to OBJECT —-> Deserialisation

YAML is a data serialisation language which is not a programming language. We can store information in it and data in it.

Markup Language is used to store documents. (HTML store documents). In YAML we can also store data and documents. That is why it is known as YAML ain’t Markup Language.

Convert the object into YAML Files —> Serialisation

Convert that YAML file back into Objects —> Deserialisation

YAML

What type of files are stored in YAML files ??? —>It is used in configuration files. Like Docker, Kubernetes, log, and Caches.

BENEFITS OF YAML

  • It is very simple and easy to read.

  • It has a strict syntax. Indentation is important.

  • Easily Convertible to JSON, XML

  • Most Languages Used YAML

  • It is more powerful when representing Complex Data

  • We can also use various tools with it like PARSERS, etc —> Various Tools Available

  • Parsing is easy —> Parsing means reading the data.

DEMO OF YEMO FILES

In YAML, We can store Key-Value Pairs.

YAML is case-sensitive.


# is used to write comment. There is no way to write multi-line comments

"Apple" : "I am a fruit"
1 : " this is my roll number"
---

#lists
-apple 
-mango
-banana
-Apple
---

# Block Styles 

cities:
 - new delhi
 - mumbai
 - gujarat 
 - himachal
---

# We can also above data in single lines 

cities : [new delhi, mumbai , gujarat, himachal]

---
{mango : "yellow fruit" , age : 50}
...

These are some of the basic examples of YAML in VSCode and we can check whether it is valid by visiting https://www.yamllint.com/

Remember that Identification and spaces are extremely important like in Python.

Differentiate Between Documents

YAML is a collection of 0 and more documents. On the above code, we have three documents which are separated by "---".

"..." Triple dot is used to make a document finish in YAML.

Converting the YAML file into JSON

You can go to https://onlineyamltools.com/convert-yaml-to-json to convert the YAML file into JSON. Below is an example of how the above document looks in json file.

{
  "cities": [
    "new delhi",
    "mumbai",
    "gujarat",
    "himachal"
  ]
}

{
  "mango": "yellow fruit",
  "age": 50
}

Datatypes in YAML

There are three ways to represent the strings in YAML.

name : Vishal Prakash
fruit : "apple"
job : 'swe'

#Storing data in multiple line 
bio : 
hey my name is Vishal Prakash.
I am a very nice dude.

# Write a single lines in multiple lines
message : >
this will 
all be 
in 
one single 
line
# this is same as:
message : this will all be in one single line

number : 5474    #int datatypes 
marks = 98.76   #float datatype

booleanValue : !!bool  No   # n, N , false , False , FALSE
#same for true -> yes , y , Y

Specifying the Data Types in YAML

We use !! to specify the data type in YAML.

# specify the type 
zero : !!int 0 
postiveNumber : !!int 45
negativeNumber : !!int -45
binaryNumber : !!int 0b11001
octalNumber : !!int 0654
hexalNumber : !!int 0x45
commaValue : !!int +25_000_000   #25,000,000
expnential number : 6.023E56

# floating Point Numbers 
marks: !!float 56.89
infinite: !!float .inf
not a num: .nan 

# null datatypes
surname : !!null Null   # or Null NULL 
~: This is a null key 


# dates and time 
date: 2002-08-28
time: !!time stamp 2:59:30.89

Advance Datatypes and a lots more in the code Snippets :

student : !!seq
 - marks
 - name 
 - roll_no

 #some of the key of the sequence will be empty known as the sparse sequence
 sparse seq:
  - hey 
  - how 
  - 
  - Null
  - sup 


  # nested sequence
-
 - mango
 - apple 
 - banana
-
 - marks
 - roll_num
 - date

 # Maps DataType 
 # Key Value Pairs are called maps
 !!map

 #nested mapping : map within a map  
 name: Vishal Prakash
  role: 
    age: 89
    job: student 

#same as 
name: Vishal Prakash
  role: {age: 89 job: student }



  # Pairs : Key may have  duplicate values . 
  # !!pairs
  pair example: !!pairs
   - job: student
   - job: teacher
   #this will be a hash table containing array(Array of hash table)

   #same as 
   pair example: !!pairs [job: student, job: teacher]


   # !!set will allow us to have unique values 
   name : !!set
    ? Kunal 
    ? Apporv
    ? Vishal 
    ? Rahul 


# dictionary !!omap 
People : !!omap
  - kunal: 
     name: Kunal Kushwaha
     age : 78
     height : 678
  - Vishal:
      name: Vishal Prakah
      age : 55
      height : 650



# Reusing some properties with anchors
likings: &likes
    fav fruits: mango
    dislikes : grapes

person1: 
  name: Kunal Kushwaha
   fav fruits: mango
    dislikes : grapes

person2: 
  name: Rahul
   <<: *likes
   dislikes : berries

person3: 
  name: Apporv
   <<: *likes

Storing Data in XML

XML - It stands for Extensible Markup Language.

It is used to store data and we can share data across various platforms. It is also used in the backend another tech stack.

XML is not readable by humans. That's by we prefer YAML.

<?xml version="1.0" encoding="UTF-8"?>
<School name="DPS" pricipal="Someone">
    <Students>
        <Student1>
            <rno>23</rno>
            <name>"Vishal Prakash"</name>
            <marks>94</marks>

        <Student1>

    </Students>

</School>

Storing Data in JSON

JSON- JavaScript Object Notation

It is heavily used in JavaScript and is one of the popular serialisation formats that people use.

These are objects. In MongoDB, we use JSON objects.

So, If we want to write this in code format, we use JSON for that.

{
    "School": [
        {
            "name": "DPS",
            "principal": "Someone",
            "Students":[
                {
                    "rollno": 12,
                    "name" : "Kunal Kushwaha",
                    "marks": 96

                },
                {

                }
            ]
        }
    ]

}

The same can be written in YAML as :

---
School:
- name: DPS
  principal: Someone
  Students:
  - rollno: 12
    name: Kunal Kushwaha
    marks: 96
  - {}

Some of the Most Important YAML tools of DevOps to Validate YAML files

  1. Datatree : https://www.datree.io/?utm_source=youtube&utm_medium=influencer&utm_campaign=kunal

  2. Monkie

  3. Lens: https://k8slens.dev/?utm_source=CloudNativeHackathon&utm_medium=Youtube&utm_campaign=DevOpsBoot

That's all.

You are now Ready to go to the next steps on the Way to learning DevOps.

/

0
Subscribe to my newsletter

Read articles from Vishal Prakash directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Vishal Prakash
Vishal Prakash