A Look into YAML!
Tags: devops, opensource, java, github
Published: 2022-03-01 18:01:47

"_YAML “YAML Ain’t markup language” a funny abbreviated form yet true!_"

## **What we'll look into?**

1. Introduction.
2. Data Serialization and deserialization
3. Benefits of YAML
4. YAML-list, key-value pairs, and more
5. Comparing Syntax with JSON
6. Data types
7. Advanced Datatypes
8. Storing data in XML and JSON files
9. YAML DevOps tools


## Introduction

YAML was initially known as Yet another markup language and was latter changed to YAML ain't markup language. So let's give our brains a little thought what is a markup language!?,the first thing which comes to our mind is HTML which gives the parent child kind of a relationship; the first header then lists, bullet points, and inside them the paragraph so on. This layered structure of putting up information is something that we see under the hood of web pages over the internet.YAML isn't a markup language because it can store data of objects whereas HTML stores only documents.If you aren't able to understand what an object is,its something which is present in real life which derieves specific atrributes from a template or a class. Example BMW is an object and Car is the class,every car made in the world has some basic things in common such as brakes and accelerators which are the attributes derived from the Car class.

## 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. So to phrase what a YAML is, it's a basic human-readable language used to represent data or store information about some configuration. In YAML you can store data and not commands.

For example, we've created an object of the patient in the memory which consists of their present health info 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 is 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. Data objects here could be thought of as information stored in a device in an application.




![Block diagram](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/asydaj8j09mqzum8sl9p.jpeg)


To put it in simple words, the data object that we have is passed to a serializer which in turn converts it into a stream of bytes or chunks of data that could be stored in a database or memory format called **YAML**.

So 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. This form could be either a YAML file or some database.

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, XML**.

For example, think of deployment or cloud-native platform in such platforms you create a data object, now assume you have your application developed and you want them to be deployed/run on 10 servers where each server can handle a fixed number of applications, here the platform accepts your request to create an object but it needs some form of code format file which provides the information about it to do the task. **This written format is called YAML**. One such platform which does this is **Google's Kubernetes**. It can be written in other languages too like Java, Python, etc but you will need separate clients for it.

It's widely used in different configuration files, dockers, Kubernetes, logs, caches, etc.

## 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 use it.

- Powerful to represent complex data.

- Use various tools available like parsers(data readers).


## Demo YAML file(.yaml or .yml files)

1. YAML is case-sensitive and indentation sensitive.
2. Contains different objects.
3. Different documents separated using **'---'**.
4. End the document using **'...'**
5. Use the [YamlLint](http://www.yamllint.com/)  website to check if it's a valid YAML code.
6. Use [this link](https://onlineyamltools.com/) to convert YAML to JSON and vice versa.
7. Block style is indented by spaces.


> ![YAML Snippet](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dr0e5irrmbfwpbnjezd6.png)

 

> .![JSON Snippet](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sl6k8uhw16xsvobft4fb.png)



## Datatypes In YAML



**Different ways to write strings are:**


> ![string datatype](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nr1by5fallz6m5v4om8g.png)




**Invalid way of writing is:**

> ![Invalid way](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4emmn966t2ym0fallvtl.png)


> ![check1](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/c5obrb3xw47sy04o5jet.png)





**A valid way of writing:**

> ![valid way](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ewcl81eg8ijjylwpsy70.png)


> ![check valid](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/r2bcgobe417ie63ot13v.png)

 



**Write a single line in multiple lines**

> ![multiple line](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ihira15tlxsswfbxr5k1.png)




**Yaml automatically detects datatypes:**

> ![dtype](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mvujxxnbv9sl6scas9sr.png)





**Specify the type**


> ![dtype1](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ravpesg4c453ll6fa7bs.png)



> ![dtype2](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4arwztbf1ek1ryoigjsc.png)
 

> Try out for null key and also think how we could represent arrays and date-time formats

**Date and time**

> ![dype4](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hrouoc5166e5yf7izezk.png)





## Advanced Datatypes


**Sequence Datatype**


> ![seq_data](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/v37f9g894ly2423l2lor.png)


**Sparse Sequence**


> ![sparse_img](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0ur9onqona9buwrhz8pk.png)


**Nested Sequence**


> ![nest](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jobdikr2tr2epmww4fbz.png)


**Key-value pair maps**


> ![map1](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5v6gtavs1u2g0gh9f7gv.png)


**key with duplicate values**


> ![dup](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jh2gpvb3ym7zx99z71w3.png)


**Set**


> ![set](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qi6ydjod1sk2scsrlv3a.png)


**Dictionary**


> ![dict](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zjbkp7w5cnf2h0rrs2hl.png)
 
 

## Storing data in XML

Extensible Markup Language. It's similar to HTML except for the fact that it doesn't have predefined tags as in HTML. We design our tags based on our needs. It's not easily understandable that's the reason why we prefer YAML files.



> ![storexml](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tnlmen3932sb64mg402h.png)


## Storing data in JSON



> ![jsonstore](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fjpj0gtdzs0b2agn6qk9.png)






## Tools and their reference links

> [Lens](https://k8slens.dev/?utm_source=CloudNativeHackathon&utm_medium=Youtube&utm_campaign=DevOpsBoot)
> [Datatree](https://datree.io/?utm_source=youtube&utm_medium=influencer&utm_campaign=kunal)
> [YamlLint](http://www.yamllint.com/)
> Use [this link](https://onlineyamltools.com/) to convert between YAML JSON  and XML
> Refer to this [blog]
(https://spacelift.io/blog/yaml) for more better explanation!