DEV Community

Cover image for A Look into YAML!
Kishore
Kishore

Posted on • Edited on

A Look into YAML!

"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

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 website to check if it's a valid YAML code.
  6. Use this link to convert YAML to JSON and vice versa.
  7. Block style is indented by spaces.

YAML Snippet

.JSON Snippet

Datatypes In YAML

Different ways to write strings are:

string datatype

Invalid way of writing is:

Invalid way

check1

A valid way of writing:

valid way

check valid

Write a single line in multiple lines

multiple line

Yaml automatically detects datatypes:

dtype

Specify the type

dtype1

dtype2

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

Date and time

dype4

Advanced Datatypes

Sequence Datatype

seq_data

Sparse Sequence

sparse_img

Nested Sequence

nest

Key-value pair maps

map1

key with duplicate values

dup

Set

set

Dictionary

dict

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

Storing data in JSON

jsonstore

Tools and their reference links

Lens
Datatree
YamlLint
Use this link to convert between YAML JSON and XML
Refer to this blog for more better explanation!

Top comments (0)