Photo by Jefferson Santos on Unsplash
Complete YAML Tutorial | What is YAML ? YAML tutorial | Benefit to use YAML in 2022
What is YAML ? | YAML tutorial | Benefit to use YAML | YAML in 2022
Table of contents
- 1. What is YAML ?
- 2. Data Serialisation Language.
- 3. Where is uses of YAML?
- 4. Benefit of YAML.
- things to remember while writing YAML
- 5. Start here:
- 6. List or Array represent in YAML.
- 7. Datatype in YAML.
- 8. Specify the types of data.
- 9. Date and Time in YAML.
- 10. Advance data Type in YAML.
- 11. Pair key may have duplicate values.
1. What is YAML ?
The full form of YAML is Yet another markup language or YAML ain't markup language.
It is not a Programming language.
It use to data format exchange data similar to XML and JSON.
In YAML, you can store only data rather than commands.
2. Data Serialisation Language.
YAML, JSON, XML
Object to file-> Serialisation.
File to object-> Deserialisation.
3. Where is uses of YAML?
- Configration file-> Docker/Kubernetes, logs, cache etc
4. Benefit of YAML.
Simple and easy to read
It has a nice syntax (Indentation is Important in YAML)
Easily convertible to JSON, XML
More powerful when representing complex data
things to remember while writing YAML
YAML is a case-sensitive language, if we create a list or other things we stored data like this "apple" and "Apple" they both have different means in YAML.
---
- apple
- mango
- banana
- Apple
- For comments in YAML, we use # like as follows
#this is comment
5. Start here:
- Open you code editor whatever you like and create a YAML file with extension like .yaml or .yml both works fine. I like .yaml
hello.yaml
6. List or Array represent in YAML.
---
fruits
- apple
- mango
- banana
- Apple
So given above example contains list of fruits and you can create list like this in YAML.
(---) is use for separate the documents and (...) is use for end of document.
7. Datatype in YAML.
- string variable.
myself: Shivam Sharma
fruits: apple
Job: Swe
bio: Hey, I am Shivam
So in above example we have seen that how we can store string in YAML. Our next is numbers, How we can store number in YAML?
Numbers variable.
number: 5651334
marks: 70.8
boolean: false
In the YAML we can also specify the type of data, like int, float, boolean and so many others also.
Let's suppose we want to store 0 and some other numbers, so here we will be looking at some of the examples as follows.
8. Specify the types of data.
- So this is, that how we can specify the datatype in YAML and while reading this topic be attentive.
zero: !!int 0
possitivenumber: !!int 50
negativenumber: !!int -50
- So you can store numbers as per your requirement like the above example, now we are going to see floating point numbers.
marks: !!float 70.46
rates: !!float 5.2
CGPA: !!float 8.34
- So, now we are going to see how we can store string in YAML.
messege: !!str I am Shivam and i love to write codes
Quotes: !!str Die with memories not the dreams
- How we can store null in YAML? Let's look at this.
surname: !! null null
9. Date and Time in YAML.
date (00:00:00Z): 20021-4-25
startTime: 2022-4-08T00:00:00
10. Advance data Type in YAML.
We have already familiar about list, if you don't know so first read point 6 then come here.
Let's suppose we want to store a sequence list of student data as following.
student: !!seq
- marks
- name
- roll_no
- It can be written the same as follows. Both work fine.
student: !!seq [marks, name, roll_no]
- Empty sparse sequence. mean when you don't want to add data or value to something. you can see the following YAML code.
sparse seq:
- hey
- how
-
- null
- sup
- Nested sequence example.
---
- ~
- mango
- banana
- apple
- ~
- marks
- roll_no
11. Pair key may have duplicate values.
pair example: !!pairs
- job:Student
- job:teacher
- job:YouTube
- We can also write same as following.
pair example: !!pairs [job: Student, job:teacher, job:YouTuber]
***For more watch this video. ***