Table of contents
What exactly YAML is 🤔 ?
YAML was previously known as Yet Another Markup Language but now it's called YAML ain't markup language. It is neither a programming language nor a markup language. It is basically a data format used to exchange data. If you already know about XML and JSON files, you can relate to this. (If you don't know about it, don't worry we'll go through it in this article only.)
YAML is Data Serialization Language.
It is basically a simple Human-readable language that can be used to separate data. It is used to store information about configuration.
In YAML you can store only data and not commands.
So what is Data Serialization?
- Serialization is basically the process of converting the data objects that is present in some complex data structure into a stream of byte(or storage) that can be used to transfer this data to your physical devices.
Another Definition of Serialization:
- Serialization is Basically a process of converting this data object which is a combination of code and data into a series of bytes that saves the state of this object in a form that is easily transmittable.
You can Visualize the below Serialization:
Note: Object is the Combination of code + data, It is basically a data storage unit.
What is Deserialization?
- The reverse of data serialization is called Data Deserialization.
Examples of Data Serialization Languages :
- XML
<?xml version="1.0" encoding="UTF-8"?>
<School name="NESS" principal="Someone">
<Students>
<Student>
<rno>38</rno>
<name>omkar Kulkarni</name>
<marks>100</marks>
</Student>
</Students>
</School>
- JSON
{
"school":[
{
"name": "NESS",
"principal": "someone",
"students": [
{
"rno": 38,
"name": "omkar kulkarni",
"marks": 38
}
]
}
]
}
- YAML
School:
- name: NESS
principal: someone
students:
- rno: 38
name: omkar kulkarni
marks: 90
So you can see above exactly related to syntax. So Syntax of YAML is human-readable and simple.
Let's learn more about YAML syntax😃
First of all the extension of the YAML file is .yaml or you can use .yml
Datatypes in YAML:
- String
#string: This is a comment
myself: "Omkar kulkarni"
job: "student"
fav fruit: "mango"
bio : |
hey my name is omkar
I am a very nice dude
# write a single line in multiple lines
message: >
this will
same as
above
# same as
message: !!str this will all be in one single line
- Numbers and boolean values:
number: 5473
marks: 98.76
booleanValue: !!bool No
# n, N, false, False, FALSE
# same for true -> yes, y, Y
If you want to specify a specific type then :
# specify the type
zero: !!int 0
positiveNum: !!int 45
negativeNum: !!int -45
binaryNum: !!int 0b11001
octalNum: !!int 06574
hexa: !!int 0x45
commaValue: !!int +540_000 # 540,000
exponential numbers: 6.023E56
---
# floating point numbers
marks: !!float 56.89
infinite: !!float .inf
not a num: .nan
Null value:
# null
surname: !!null Null
# or null NULL ~
~: this is a null key
What about the Date & time:
# dates and time
date: !!timestamp 2002-12-14
india time: 2001-12-15T02:59:43.10 +5:30
no time zone: 2001-12-15T02:59:43.10
Let's Look at some advanced Data Types:
All related to sequence:
student: !!seq
- marks
- name
- roll_no
# like this also
cities: [Delhi, Mumbai, Pune]
# some of the keys of the seq will be empty
# sparse seq
sparse seq:
- hey
- how
-
- Null
- sup
# nested sequence
-
- mango
- apple
- banana
-
- marks
- roll num
- date
Key-value Pairs:
# pairs: keys may have duplicate values
# !!pairs
pair example: !!pairs
- job: student
- job: teacher
# same as
pair example: !!pairs [job: student, job: teacher]
# this will be an array of hashtables
Set:
# !!set will allow you to have unique values
names: !!set
? Omkar
? Nishant
? Aditya
Dictionary:
# dictionary !!omap
people: !!omap
- Omkar:
name: Omkar Kulkarni
age: 21
height: 678
- Nishant:
name: Nishant OP
age: 50
height: 456
reusing some properties using anchors
likings: &likes
fav fruit: mango
dislikes: grapes
person1:
name: Omkar Kulkarni
<<: *likes
person2:
name: Nishant
<<: *likes
dislikes: berries
# this will look like
person2:
name: Nishant
fav fruit: mango
dislikes: berries
person3:
name: Aditya
fav fruit: mango
dislikes: grapes
Congratulations🎉 You are done with the syntax of YAML.
Uses of YAML:
- Importing and exporting data to and from the server.
- Configuring files can be written in YAML.
- Transferring data between two different components of the application.
- Intermediate data storing.
Benifits of YAML:
- Simple and easy to read.
- Nice and Strict Syntax.
- Most Languages use it.
- More powerful when representing complex data.
Note: Any JSON input can be converted to YAML output and vice versa
Some important points you need to know about YAML!
- YAML is case-sensitive [apple != Apple]
- In YAML, we use spaces for indentation and not TABS.
- In YAML, there are no multi-line comments. (only single-line comments are available)
- To separate blocks of code treat them as documents un YAML file, use ---
To make like the end of document use ... - Some of the keys of the sequence will be empty and are known as the Sparse sequence.
❤️ Thank you very much for reading ❤️
If you find the blog useful don't forget to like, share and comment.
If I miss out on anything feel free to comment
Feel free to comment your views in the comment section.
Connect with me on Twitter
Happy Learning 😊!