Introduction to YAML

Introduction to YAML

What is YAML?

YAML Ain't Markup Language previously known as Yet Another Markup Language.

YAML is a human-readable data-serialization language. It is commonly used for configuration files and in applications where data is being stored or transmitted.

  • Basically, it is a simple human-readable language that can be used to store and exchange data. similar to XML and JSON.

-Storing of the data in the files is known as the data serialization. let's look into this.

Why has YAML's name changed from yet another markup language to YAML ain't markup language?

  • The reason is that markup language only stores the document but YAML stores both the document as well as the object.

  • YAML is a case-sensitive language.

  • # is used to comment in YAML.

Benefits of YAML

  • It is simple and easy to read.

  • It follows a strict syntax format, where indentation (like space before text) is a must and important.

  • it can be easily converted into JSON and XML.

  • Parsing (reading data) is easy. and more powerful when representing complex data.

What is Data serialization?

It is the process of converting a data object (code + data) into a series of bytes that saves the state of this object in an easily transmittable form.


The opposite process is known as De-serialization.


why Data serialization?

  • Suppose have multiple applications made of different tech stacks and you want to transfer the object (code + data) from one application to another without changing its data while transmitting from one application to another, so here the data serialization helps in transmitting, reconstructing, and using the object in different applications by using a single data format which can be used in all the applications.

  • So if you want to represent this object into a file that you can read, code, and modify into those files then data serialization will help, and the language used to represent this object datatype in text format is known as Data serialization languages. examples are YAML, JSON, XML.

  • the extension for YAML file is .yml or .yaml and for JSON file it is .json.

  • YAML stores some datatypes as documents and each document can be differentiated using (---) and it ends with (...)

Key-value pairs or Key datatypes


For example

"name": "gaurav"

here name is a key and the value is gaurav.

lists datatype

- item 1
- item 2 
- item 3

Block style eg:

  - mango
  - apple
  - grapes

fruit: [mango, apple, grapes] //flow style in order to avoid the bad indentation

Datatypes in YAML

  1. YAML automatically specifies the datatypes
  • String variable

There are three ways to represent strings:

name: "Gaurav"
name: 'Gaurav'
name: Gaurav
  • String data in multiple lines

    Bio: |
    Here this line now passes into an object without any error.
    and in one line as well. we used the' | ' sign.
  • String data in single line

message: >
this will
all be
in one single line.

#same as 
message: this will be in one single line.
  • integer datatype

    number: 123456
  • float datatype

    marks: 40.45
  • boolean datatype

    booleanValue: No, N, n, False, false / Yes, Y, y, True, true

2 . Specifying the datatypes in YAML


Syntax --> variable: !!datatype value

for example:

zero: !!int 0
positiveNum: !!int 34
negativeNum: !!int -34
binaryNum: !!int 0b11001
OctalNum: !!int 06754
hexa: !!int 0x45
commaValue: !!int +54_000  // 54,000
exponential numbers: 2.034E56

floating point numbers !!float
marks: !!float 12.34
infinite: !!float .inf
not a num: .nan

booleanValue: !!bool no/yes
string: !!str

null or none !!null
surname: !!null Null ~
~: this is null key

3 . Advanced datatypes

  • Sequence datatypes Syntax:
    variable: !!seq
student: !!seq
  - marks
  - roll-no
  - name

student: [marks, roll-no, name]
  • Sparse sequence some of the keys of the sequence is empty

    sparse seq: !!seq
     - item1
     - item2
     - item3
  • Nested sequence used to represent sub-items in the list

nested seq: !!seq


   - school
   - marks
   - class
   - name
  • Maps Datatypes key-value pairs are called maps. !!map

  • nested mapping like Map within a map. example:

name: !!map Gaurav
role: !!map
        age: 18
        job: student

#Same as 
name: Gaurav
role: [age: 18, job: student]
  • Pairs datatypes Keys may have duplicate values !!pair examples:

    pair example: !!pair
       - job : open source contributor
       - job : student     
    #this will be an array of hashtables
    #same as
    pair example: **!!pair** [job: open source contributor, job: student]
  • Set datatypes it will allow you to have unique values. !!set examples:

    name: !!set
        ? John
        ? David
  • Dictionary datatypes It is a collection of key-value pairs and each of the key-value pairs can be nested with a lot of options/sequences. !!omap


people: !!omap
            name: Gaurav
            age: 18
            name: Harsh
            age: 18
  • Reusing some properties using anchors

Anchors: A feature that let you identify an item and then reference it elsewhere in your life. OR what do you want to copy and where do you want to copy it it is represented or created by using the " & (ampersand) " sign.

<<: inserts the contents of that object

Likes: &likes
 - Fruits: Watermelon
 - Food: chole bhature

 -Name: Gaurav
 <<: *likes

Storing Data with Real-Life Examples

  • With JSON
  • With YAML
     - name: DPS
     principal: Someone
         -rno: 12
         -name: Anyone
         -marks: 67
  • YAML DevOps Tools


Monokle by Kubeshop