YAML for Everyone: The Simple Guide to YAML

YAML for Everyone: The Simple Guide to YAML

In today's world of software development, configuration files are an integral part of any project. They serve as a blueprint for setting up and customizing applications, ensuring consistency and ease of maintenance. One such configuration file format that has gained significant popularity is YAML (YAML Ain't Markup Language). YAML is a human-readable data serialization format, widely used for configuration files, data exchange, and even as a markup language for structured documents. Its simplicity and flexibility make it a favorite choice among developers, system administrators, and DevOps professionals.

This blog post aims to provide a comprehensive guide to YAML, catering to everyone from beginners to experienced developers. Whether you're new to YAML or looking to enhance your knowledge, this guide will walk you through the essentials of YAML, including its syntax, properties/datatypes, and useful tools that can boost your productivity.

What is YAML ?

YAML was previously known as "Yet Another Markup Language". But now it is called "YAML ain't Markup Language".

It is not a programming language. It is a data format used to exchange data. It is similar to XML and JSON datatypes.

It is a simple human-readable language that can be used to represent data. It is used to store some information about configurations.

💡
In YAML you can store only data, not commands.

What is DATA SERIALIZATION?

Serialization is a process of converting the data objects that is present in some complex data structure into a stream of byte (or storage) that can be used to transfer this data on your physical devices.

Object -> File = Serialization

File -> Object = Deserialization

💡
The object is the combination of code + data. It is a data storage unit.

Another Definition of Serialization :

Serialization is a process of converting this data object which is a combination of code and data into a series of bytes that saves the state of this object in a form that is easily transmittable.

Deserialization :

The reverse of data serialization is called deserialization.

Data serialization Languages :

  • YAML

  • JSON

  • XML

Why YAML is not known as the Markup Language? Why did the full form change?

Markup languages are used to store only documents. But in YAML you can store object data along with documents. That is why it is known as YAML ain't Markup Language.

Uses of YAML File :

  • Configuration files - Docker, Kubernetes etc.

  • Logs, Caches etc.

Benefits of YAML :

  1. It is simple and easy to read.

  2. It has a strict syntax - Indentation is Important.

  3. Easily convertible to JSON & XML Files.

  4. Most languages use YAML.

  5. It is more powerful when representing complex data.

  6. You can use it with various tools like parsers etc.

  7. Parsing is easy. (Parsing means reading the data)

Some Important Points:

  • YAML is case-sensitive. { Apple != apple }

  • In YAML we use spaces for indentation and not TABS.

  • In YAML, there are no multi-line comments. (Only single-line comments are available).\

  • To separate blocks of code and treat them as documents in a YAML file use ---

  • To mark as the end of documents use ...

  • Some of the keys of the sequence will be empty and is known as sparse-sequence.

XML

  • XML stands for eXtensible Markup Language.

  • XML is a markup language much like HTML.

  • XML was designed to store and transport data.

  • XML was designed to be self-descriptive.

Example :

<?xml version="1.0" encoding="UTF-8"?> 
<note>
  <to>Abhishek</to>
  <from>Bhattacharjee</from>
  <heading>Reminder</heading>
  <body>Don't forget to subscribe to newsletter for awesome blogs</body>
</note>

JSON :

  • JSON stands for JavaScript Object Notation

  • JSON is a lightweight format for storing and transporting data

  • JSON is often used when data is sent from a server to a web page

  • JSON is "self-describing" and easy to understand

JSON Syntax Rules

  • Data is in name/value pairs

  • Data is separated by commas

  • Curly braces hold objects

  • Square brackets hold arrays

Example :
{
"employees":[
    {"firstName":"John", "lastName":"Doe"},
    {"firstName":"Anna", "lastName":"Smith"},
    {"firstName":"Peter", "lastName":"Jones"}
]
}

Major Concepts of YAML :

Lists:

  • Lists in YAML are represented using (-) followed by a space.
Fruits:
 - apple
 - banana
 - orange
 - Apple  // case sensitive , so { Apple != apple }

Comments:

  • Comments in YAML start with the pound (#) symbol and continue until the end of the line.
# This is comment in YAML

Inline Notation:

  • The inline notation allows you to represent data on a single line.
person: {name: John, age: 25, City: Newyork}
cities: [Mumbai, Delhi, Raipur]
💡
Always give one space after : when writing the value

Flow Notation:

  • Flow notation allows you to represent complex data structures using square brackets ([]) and curly braces ({}).
employees: [{name: John, age: 30}, {name: Jane, age: 28}]

# which is same as writing 

employees:
  - name: John
    age: 30
  - name: Jane
    age: 28

String Data types and ways of writing :

  • Strings in YAML can be written using single quotes (''), double quotes (""), or without quotes.
name: 'Abhishek'
address : "123, XYZ"
message: Hello world!

Multi-line Strings:

  • Multi-line strings in YAML can be written using the pipe symbol (|) followed by a new line and proper indentation.
description: | 
  This is a multi-line 
  String Example
💡
In any case, the lines should be aligned with the indentation of the description

Writing a single line in multiple lines using '>' :

  • The greater than symbol (>) followed by space allows you to write a single line in multiple lines while preserving line breaks.
message: >
  This is a single line
  written in multiple lines

Scalar values:

  • Scalar values are simple atomic values like numbers, booleans and Strings in YAML.
number: 43
marks: 98.5
booleanValue: false

The boolean value can also be represented as :

n, N, False, false, FALSE -> for false

y, Y, true, True, TRUE -> for true

Flow Style :

  • Flow style allows representing data in a compact form using commas and braces.
person: {name: John,age: 25, city: India}

Specifying the data types:

  • YAML allows specifying the data type of a value using the '!!'.

  • Syntax : variableName: !!Datatype value

price: !!float 9.99
zero: !!int 0
cost: !!int 99
positiveno: !!int 55
negativeno: !!int -55
booleanValue: !!bool true 
comma Value: !!int 1,00,000
expo Value: 6.23E56
binaryValue: !!binary ob11010
octalValue: !!int 0o777
hexValue: !!int 0xFF

# What does 6.23E56 mean?
# 6.23 * 10^560
💡
The key can be written with spaces & special characters.

Floating point numbers and Other types :

floatNumber: !!float 55.5
infinity: !!float .inf
negativeInfinity: !!float -.inf
not a num: .nan
booleanValue: !!bool true
stringmessage: !!str This is a string

#What does .inf mean 
# It means infinity

#What does .nan mean 
# It means not a number

.inf -> infinity

.nan -> not a number

Null values & Null keys:

  • Null values can be represented using the null, Null, NULL,~(tilde).
surname: !!null null

Dates and times:

  • Dates and times can be represented in YAML using ISO 8601 format.
date: 2017-01-01
time: 12:00:00
datetime: 2017-01-01T12:00:00 # No timezone specified
datetime: 2017-01-01T12:00:00Z # Assuming UTC timezone
datetime: 2017-01-01T12:00:00+05:30 # Assuming IST timezone

Sequence

  • The !!seq tag can be used to specify that a value should be treated as a sequence(list).
student: !!seq
 - marks
 - name
 - roll_no

 #like this also
 cities: !!seq [Mumbai, Delhi, Bangalore] #OR
 cities: [Mumbai, Delhi, Bangalore] # Inline notation

Sparse Sequence:

  • YAML allows representing sparse sequences where some elements are null or missing.

    OR

  • When we wish that some of the keys of the sequence should be empty.

sparse seq: !!seq
 - marks
 - 
 - roll_no
 - null

Nested sequence:

-
 - Apple
 - Banana
 - Orange
-
 - Red
 - Yellow
 - Blue

!!Map:

  • Which contains key-value pairs.
# nested mappings (Map within a Map)

 name: Abhishek Bhattacharjee
role: 
  age: 55
  job: Software Engineer
  company: Red Hat
  location: Bangalore
  hobbies: 
    - reading
    - writing
    - travelling
    - playing

#same as
name: Abhishek Bhattacharjee
role: {age: 55, job: Software Engineer, company: Red Hat, 
location: Bangalore, hobbies: [reading, writing, travelling, playing]}

!!pairs:

  • YAML allows representing multiple values for the same key using pairs which are represented using !!pairs.
pairs example: !!pairs
 - job: 
    - Software Engineer
    - Teacher
  # OR
  - job: [Software Engineer, Teacher] # This will be an array of hashtables  
  # OR  
  - job : Software Engineer
  - job : Teacher
  # OR
  pair example: !!pairs [job:Software Engineer, job: Teacher]

!!Set

  • The !!set tag can be used to specify that a value should be treated as a set (Collection of unique elements).
 # !!set will allow only unique values
  names : !!set
    ? Abhishek
    ? Dipika
    ? Anuradha
  # It cannot have same values  

  # what does ? means above !
  # It mean it has null values as of now

Dictionary (!!omap):

  • The !!omap tag can be used to specify that a value should be treated as an ordered map (dictionary with preserved order).
# dictionary !!omap
people: !!omap
  - Abhishek:
    - age: 55
    - job: Software Engineer
  - Dipika:
    - age: 50
    - job: Teacher
  - Anuradha:
    - age: 25
    - job: Student

Anchors (&) and Aliases (*):

  • YAML allows the use of Anchors (&) and Aliases (*) to refer to the same value multiple times within the document.
#reusing some properties using anchors
likings: &likes
  fav fruit: mango
  dislikes : grapes

person1:
   name: Abhishek
    <<: *likes
    dislikes: banana # Overriding the dislikes value here from likings 

#this will be same as
person1:
   name: Abhishek
    fav fruit: mango
    dislikes : banana  #overwrites the dislikes from the anchor    

person2:   
   name: Dipika
    <<: *likes

person3:   
   name: Anuradha
    <<: *likes
💡
<<: is used to use Anchors using Aliases.

DevOps Tools for YAML:

These are the tools that can make your life easier when you are playing around with YAML Files:

Resources to Learn and Practice YAML :

This blog covers the entire YAML required for you to work in Cloud Native landscape. If you want a detailed blog on the use case of the mentioned DevOps Tools then please let me know in the comments.

Also if you like the blog don't forget to ❤️ and follow me for such awesome blogs. Your support encourage me to write more such blogs. Happy reading!✨