Table of contents
No headings in the article.
Let's start getting info about YAML by firstly knowing its full form. YAML stands for YAML ain't markup language.
What is YAML?
- YAML is actually a data format used to exchange data.
- It is similar to XML and JSON data-type.
- So, YAML is a simple human-readable language that can be used to represent data.
- In YAML you can store only data and not commands, unlike other programming languages.
Data Serialization and Deserialization
Serialization -
This is actually the process of converting the data objects into complex data structures or streams of bytes. Basically, data objects present in complex data structure into the stream of storage that can be used to transfer data in your physical device.
The below image can be helpful to understand this concept: π
The above thing happens on JSON too in which we call API which gets converted to JSON DATA which is more human-readable.
The above image shows how the Serialization process stores data.
- Objects are basically, code together with some data.
- Data Objects are converted into a series of bytes which saves the state of an object in a form that is easily transmittable This is known as Serialization. Fields like Big Data, Machine Learning, DevOps, etc. uses this concept Serialization Languages: JSON,XML and YAML De-serialization- The file which was converted during serialization can be converted back to an object is Deserialization. Object - > File (Serialization) File - > Object (Deserialization) See how simple could it beπ. Types of Objects YAML stores-
- Configuration Files of any tools or whatever, some are Kubernetes Config File, Flutter Pubspec YAML file, Docker, etc.
- Log files, caches, etc. Benefits of YAML
- Simple and Easy to read.
- It has a strict syntax(Mistakes aren't allowed like indentation).
- Easily convertible to JSON, XML.
- Most languages use YAML.
- More powerful while representing complex data
- Various tools are available like parsers.
- Parsing is easy.
## YAML Data Types and Real World Examples
Key Datatype
Key-Value pairs
"apple" : "I am a red fruit"
1 : "This is a roll number"
List Datatype
- apple
- mango
- banannananna
Block Style
cities:
- new delhi
- mumbai
- ahmedabad
YAML Parsers can be used to check if YAML is valid or not. One such website is YAMLLINT. Differentiating Between Documents
cities:
- new delhi
- mumbai
- ahmedabad
---
cities: [new Delhi, mumbai]
---
{mango: "fruit", age:5}
...
--- is used to differentiate the type of document. ... is used to define that document is finished now.
JSON Files Onlineyamltools is a website for conversion and stuff for YAML files.
YAML FILE π
cities:
- new delhi
- mumbai
- ahmedabad
converted to JSON π
{
"cities": [
"new delhi",
"mumbai",
"ahmedabad"
]
}
Flow Type
cities: [new Delhi, mumbai]
This doesn't give an indentation error
Comments YAML doesn't support multiline comments, we have to use the # sign
# Mango
# Apple
# Grapes
String Variables
name: Sumit Mukharjee
fruit: "Apple"
job: 'swe'
message: !!str Hello There
Write a single line in multiple lines
message: >
this will
all be
in one single line
same as This will all be in one single line
YAML auto detects data type
Specifying the data types
zero: !!int 0
positiveNum: !!int 45
negativeNum: !!int -45
binaryNum: !!int 0b11001
octalNum: !!int 06574
hexNum: !!int 0x45
commaValue: !!+540_000 # 540,000
exponentialNumbers: 6.023E56
Float Value:
marks: !!float 56.89
infinity: !!float .inf
noANumber: .nan
Boolean Value
booleanValues: !!bool No
Null Value
surname: !!null Null
~: this is a null key
DateTime in YAML
date: !!timestamp 2022-01-25
india time: 2001-12-15T02:59:43.10 +5:30
no time zone: 2001-12-15T02:59:43.10
Advanced-Data Type
When some of the sequences will be empty they are known as a sparse sequence.
```sparse seq:
- hey
- how
- Null
- sup ``` Nested Sequence sequence inside sequence
- marks
- name
- roll_no
-
- simplemarks
- avgMarks
Maps DataType key:value pairs are called map datatype Nested Maps
```name: Sumit Mukharjee role: age: 78 job: student
**Pairs Datatype**
Sometimes one key may require duplicate values
```pair examples: !!pairs
- job: student
- job: teacher
Real World Example
Storing Above data in XML
<?xml version="1.0" encoding="UTF-8">
<School name="DPS" principal="Someone">
<Students>
<Student>
<rno>23</rno>
<name>"Sumit"</name>
<marks>94</marks>
</Student>
</Students>
</School>
In JSON
{
"School": [
{
"name": "DPS",
"principal": "Someone",
"Students": [
{
"rno": 12,
"name": "Sumit Mukharjee",
"marks": 67
}
]
}
]
}
In YAML
- name: DPS
principal: Someone
Students:
- rno: 12
name: Sumit Mukharjee
marks: 67
This was it. Thanks for readingπ.