WELCOME TO MY BLOG !!
If you are a developer writing APIs, you’ve probably come across YAML even though your primary serialization tool might be JSON. YAML is quite popular in the DevOps tools. One of the main reasons why YAML popularity has increased so much over the past few years is that it is super human-readable and intuitive which makes it a great tool for writing configuration files for DevOps tools like Docker, Kubernetes, Ansible, Prometheus, etc.
Let’s learn the basics of YAML.
What is YAML ?
YAML is a data serialization format that stands for YAML ain’t Markup language.
The main advantage of using YAML is readability and writability. If you have a configuration file that needs to be easier for humans to read, it’s better to use YAML. YAML is not a complete substitution for JSON as JSON and XML have their places too; nevertheless, it’s useful for learning YAML.
Another benefit of YAML is its support of various data types like cases, arrays, dictionaries, lists, and scalars. It has good support for the most popular languages like JavaScript, Python, Ruby, Java, etc.
YAML only supports spaces, and it is case-sensitive as well as space sensitive. Tabs are not accepted universally. A YAML file has .yaml
extension.
Basic YAML syntax
Every YAML starts with ---
which denotes the start of a YAML file.
When creating an API, we’re interested in a feature provided by YAML known as mapping.
The following examples show the example of mapping in YAML.
---
name: James
boy: yes
GPA: 3.41
The mapping syntax is key: value. (Note the space, it’s very crucial in YAML, unlike JSON or XML.
What is Data Serialization?
But what is YAML? YAML isn't a programming language not really a markup language too but is a data format used to exchange data which is similar to XML and JSON data types.
For example, we've created an object of the patient in the memory which consists of their present health info such as SPo2 level, heart rate, blood pressure, etc. Now we want to store this information or share it in an android app, a web app or say a machine learning app; here it's necessary or fundamentally important to have one single format in which data could be shared across the different applications.
So how does this happen? this is where Serialization and Deserialization come. Serialization is a process of converting data objects that is present in some complex data structures into a stream(chunks of data) of storage, that can be used to transfer your data as in your physical devices.
To put this up in a simpler manner, Serialization is the process of converting this data object which is the combination of code and data into a series of bytes that saves the state of the object in a form that is easily transmittable.
In layman's terms, it's a file where you store your object to read, code, and modify to represent data in code format(YAML) so a common format is maintained. Then you can take this file and convert it back into an original object format which is called deserialization. And the languages used in these files are called Data serializable languages.
Some Data serializable languages are YAML, JSON, and XML.
Benefits of YAML
Simple and easy to read.
Strict and easy syntax with importance to indentation.
Easily convertible to other serializable languages.
Its-Popular as most languages uses it.
Powerful to represent complex data.
DataTypes in YAML
In YAML, there are three types of primitive datatypes
Scalars
List
Mappings (Key-Value pairs)
Scalars
Scalars are data that can be identified as a single value.
The value of the scalar can be integer, float, Boolean, etc.
Here is an example of Scalars data types :
---
MALE: FALSE
GPA: 3.61
ISSUES: NULL
NAME: “BIGYAN”
AGE: 16
Lists
Lists are very important in YAML.
In YAML, a list or collection of values can be represented in two ways :
block style
flow style
Here is an example of block style :
---
fruits:
- apple
- banana
- mango
- Here is an example of flow style :
---
fruits: [ apple,banana,mango ]
- Here is an example of nested list :
Subjects:
Engineering:
Mechanical engineering:
Design and manufacture
Automobile
Control and Design
Civil engineering:
Structural engineering
Hydropower
Arts:
Medieval
Modern
Painting
Mappings
Mappings (or hashes, dictionaries) are unordered sequences of key/value pairs.
- Keys in a dictionary must be unique.
- Here is an example:
character:
name: rahul
subject: maths
YAML also provides &
and *
symbols as anchors and references to the anchor to avoid duplication. They are essential in configuration files in frameworks like Ruby on Rails to make the YAML file smaller.
See the example below
<span class="hljs-attr">details:</span> <span class="hljs-meta">&details</span>
<span class="hljs-attr">name:</span> <span class="hljs-string">"John"
</span> age: 18
profession: engineer
<< : * details
which is equivalent to:
profession: engineer
name: "John"
age: 18
Conclusion
In modern programming frameworks and applications where data is stored or distributed, YAML is becoming increasingly common in configuration files.
YAML targets many of the same communication applications as Extensible Markup Language (XML) but has a minimal syntax that is deliberately different from XML.
YAML files can be created for fixed data structures using print commands that write both the data and the YAML’s particular decoration. However, a dedicated YAML emitter is preferable for dumping various, or complex, hierarchical data. Similarly, with regular expressions, basic YAML files (e.g., key-value pairs) are readily parsed.
AND THAT'S IT !!!
Hope you got the basic knowledge about what YAML is and its benefits.
Enjoyed reading the Blog? Do comment down below 😊