Everything you need to know about YAML

Everything you need to know about YAML

YAML

Yet Another Markup Language (Previous Full-Form)

YAML Ain't Markup Language (Present Full-Form)

What is YAML?

  • A simple human-readable language that can be used to represent data
  • YAML is not a programming language
  • It is a data format used to exchange data
  • Similar to JSON & XML
  • Used to store some information about some configurations
  • In YAML, you can only store data, not commands.

Data - Serialization

Serialization:

Serialization is the process of converting the data object that is present in a complex data structure into a steam of bytes(or storage) that can be used to transfer this data on your physical device. or A process of converting this data object into series of bytes tat saves the state of the object which is easily transmittable.

image.png

Deserialization:

The reverse of data serialization is data deserialization.

image.png

Object:

Combination of code and data. It is basically a data storage unit.

Some Data - Serialization Languages

  1. YAML
  2. XML
  3. JSON

Why YAML is not considered as a markup language and why it's full form changed?

Markup language are used to store only documents but in YAML, you can store object data along with documents. That's why it is not considered as a Markup Language.

Uses of Data Serialization:

  • Used in configuration files, such as Docker / Kubernetes, etc.
  • Logs, caches, etc.

Benefits of YAML:

  • It is simple and easy to read
  • It has a strict syntax. (Indentation is important)
  • Easily convertible to JSON, XML
  • Most languages use YAML
  • It is more powerful when representing complex data
  • Parsers, etc. various tools available
  • Parsing is easy. (Parsing means reading the data)

Some Important Points:

  • YAML is case senitive. [apple != Apple]
  • In YAML, we use spaces for indentation not Tabs
  • In YAML, there is no multi-line comments. (only single is comment is available)
  • To seperate blocks of code and treat them as documents in a YAML files use( --- )
  • To mark as the end of document use ( ... )
  • Some of the keys of the sequence will be empty and is known as sparse sequence.

YAML Syntax

#key-value-pairs
"apple" : "This is a red fruit" 
1 : "This is my roll no."

---

#lists
- apple
- horse
- Sourav
- sourav

---

#block
cities:
 - New Delhi
 - Mumbai
 - Pune
 - Banglore

--- 

cities: [New Delhi, Mumbai, Pune, Banglore]

---

 {mango: "Yellow Fruit", qualtily: "A+"}

...

Datatypes

#string variables
myself : Sourav
fruit : "Orange"
job : 'DevOps Engineer'

---

#multiple line strings

bio:  |
 Hey, I'm Sourav Dhiman
 I'm a DevOps Engineer

--- 

#write a single line in multiple lines

message: > 
 This is written 
 in multiple
 lines but it 
 will show in single line

---

numbers: 3942
makrs: 84.32
booleanvalues: No #N, n, false, False, FALSE
#same for true - Y, y, yes, True, TRUE

#specify the type

zero : !!int 0
positivenum: !!int 34
negativenum: !!int -12
binarynum: 0b11001
octalnum: !!int 043423
hexalnum: !!int 0x45

#floating point number

marks: !!float 32.38
infinite: !!float .inf
not a num: !!float .nan

#null

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

Advanced Datatypes

student: !!seq
- marks
- name
- roll_no

#like this also
student: [marks, name, roll_no]

#some of the keys of the seq will be empty
#sparse seq
sparce seq:
- hey
- how
- 
- null
- sup

#nested seq
liking:
- fruits:
   - banana
   - apple
- vegetable:
   - sweet pototo
   - tomato   

#key: value pairs are called maps
!!map

#nested mapping: map within a map
name: Sourav
role:
  - age: 19
  - job: student

#same as
name: Sourav 
role: {age: 19, job: student}

#pairs: keys may have duplicate values
!!pairs

pair examples: !!pairs 
 - job: student
 - job: teacher

 #same as

pair example: !!pairs [job: student, job: teaacher]
#this will be an array of hastables

# set will allow you to have unique values

Idenity:
 ? Sourav
 ? Nikhil
 ? Sidhant

 #dictionary !!omap

 people: !!omap
  - Sourav:
     - name: Sourav Dhiman
     - age: 19
     - height: 176
  - Nikhil:
     - name: Nikhil Sharma
     - age: 21
     - height: 174


# reusing some properties using anchors

likings: &likes
 fav fruit: mango
 dislikes: grapes


person1:
 name: Sourav 
 <<: *likes

person2:
 name: Nikhil
 <<: *likes
 dislikes: oranges

#this will look like

#person2:
 name: Nikhil
 fav fruit: mango
 dislikes: oranges

person3:
 name: Sidhant
 <<: *likes

Did you find this article valuable?

Support Sourav Dhiman by becoming a sponsor. Any amount is appreciated!