YAML Tutorial – A beginners guide
YAML or Yet Another Markup language is a data serialization language that’s mostly used as an alternative to JSON or XML in order to establish connections among different tech stacks. This tutorial covers the YAML tutorial with amiable examples.
Unlike Extensible Markup Language it is more likely human legible, making it easier to compose and more leisurely to go through.
Notations
- Indentation is really important
- A specific data type can have multiple declaration style
- YAML is a superset of JSON
- It doesn’t contain any command
- YAML is also addressed as YML
- Mostly used to create configuration files to be used by automation tools
Initialization
workspace:
working_on: "NodeJS"
version: 1.0.0
is_easy: true
release: 2022-05-29 23:18:10
To create an object we start with the following syntax, followed by a colon, and then by indenting lines.
Strings are kept inside inverted commas, while numbers do not necessarily need commas.
Arrays
tags:
- Javascript
- NodeJS
- MongoDB
Arrays are still needed to be followed by indented, a simple -
is used to denote array elements. More than one types of syntax are possible while creating an array.
tags: ["Javascript", "NodeJS", "MongoDB"]
Objects & Arrays
details:
- name: "Sam"
email: "sam@nated.in"
role: "HR"
- name: "Taylor"
email: "taylor@nated.in"
role: "Sales"
- {name: "admin", email: "admin@nated.in", role: "cyber security"}
Objects inside an array can be assigned with the format [{}, {}, {}]
.
The third element in the following array is written with the second type of array syntax.
Description
short_desc: >
It will remove all spacing
and white spaces while using the greater than sign
Folded Style: The short description is written by the Description title and then sign >#
followed by the description itself. A short description doesn’t follow any spacing and syntax, it removes all the spacing in the description.
long_desc: |
By using the pipe symbol
You can have the indentation
And spacing in your description
Literal Style: A Long description is written by title and the sign |
followed by the description. By using the sign YAML (opens in a new tab) follows all the spacing and indentation in the written text.
Anchors
&ver 1.0.0
show_version: *ver
With the anchor denoted by a &
symbol, a value is assigned to the variables. Later by using *
followed by the variable name we can access the variable value.
Inventors:
elon: &about
job: Inventor
worth: Billions
tony:
<<: *about
invention: Arc Reactor
The about variable contains ‘job’ and ‘worth’ data from Elon’s information list. If another person Tony is having the same Job and same Worth, you can simply import and expands current information about Tony from previously written information by anchors.
Anchors in this scenario help skip repetitive tasks, and Tony later can have extra skills apart from Elon, like ‘Invention’ in this example.
Type casting
age: !!float 11 # 11.0
gpa: !!str 9.5 # "3.5"
Type casting is used to convert a data type into another, age initially had 11 as an integer though after using !!float
, it’s converted to a float number. Gpa was a float value though using !!str
, it converts itself to a string value.
Practical Example
employees:
- soumya:
name: Soumya Mondal
job: Developer
skills:
- python
- Javascript
- cpp
- elon:
name: Elon Musk
job: Inventor
skills:
- python
- aeronautics
- ev
This is a complex example of a user management system where objects inside arrays are introduced. A nested array is used to denote multiple users through elements and then skills in another separated array.
Also Read: Top Future proof Skills that you should learn (opens in a new tab)
The below following snippet is an example of the same information written in Extensible Markup Language or XML.
<Employee>
<soumya>
<name>Soumya Mondal</name>
<job>Developer</job>
<skills>
python, javascript, cpp
</skills>
</soumya>
<elon>
<name>Elon Musk</name>
<job>Inventor</job>
<skills>
python, aeronautics, ev
</skills>
</elon>
</Employee>
Conclusion
YAML has a pretty easy syntax to follow. It is been used in the majority of the cloud technologies from Docker, Kubernetes, Ansible, Jenkins, etc. YAML is heavily used in DevOps (opens in a new tab) so it’s always good to have an up by learning it. This was a quick and short tutorial on “YAML Tutorial – A beginners guide“.
I hope this has helped you, if you’re having any trouble comment down and we’ll help you as soon as possible.