A Quick Introduction to YAML

A Quick Introduction to YAML

Introduction

YAML is a simple human readable language to represent data. YAML stands for " YAML Ain't Markup Language". Previously it was known as "Yet Another Markup Language".

But, It is neither a programming language nor a markup language, It is actually a Data Serialization Language.

  • YAML is used to store information about different things, we can use YAML to define key-value pairs things variables, list, objects.

  • YAML is very similar to another language called JSON (Javascript Object Notation), but YAML emphasis on Readability and user friendliness like super readable and super clean.

  • We can define YAML files in two different extensions .yaml or .yml.

  • The --- symbol mark represents the start of a yaml document.

  • The ... symbol mark represents the end of a yaml document.

Data Serialization

  • Data Serialization is is the process of converting a data object into a byte stream that can be used to store, transfer and distribute on physical devices.

  • The reverse process of Data Serialization is know as Data Deserialization.

  • Examples of Data Serialization Languages: YAML, XML, JSON

image.png

Comment in YAML

  • There is no multiline comment in YAML, only Single line comment.
# This is a comment

Data Types in YAML

String

  • We can define a Strings in 3 ways:
# String Datatype
firstName: "Sanyasee"
lastName: 'Sahu'
occupation: Software Developer

# we can specify the type also
message: !!str "Hello! How are you?"

Numbers

  • We can directly use numbers (YAML will automatically detects it) or we can specify the type also.
# Numbers

age: 24
cgpa: 8.26
favNumber: 1e+10 # Exponential value


# specify the type 

number: !!int 17
commaValue: !!int 1_54_000  # 1,54,000
mark: !!float 90.50

Booleans

  • Booleans are True/False Values.
# Booleans

male : true
sleepy: false

Dates

  • All dates & date times inside YAML are gonna be defined using the ISO 8601 Standard.
# Dates

birthDay: 1998-06-14 09:10:20   # ISO 8601

Null Value

# Null value
flaws: null

Objects

  • We can store all these values into an object, using indentation (space).
# Store all informations into an Object

person: 
  firstName: "Sanyasee"
  lastName: 'Sahu'
  occupation: Software Developer
  age: 24
  cgpa: 8.26
  favNumber: 1e+10
  male : true
  birthDay: 1998-06-14 09:10:20
  flaws: null

Lists

  • We can store a list of items.

  • Each item in the list is gonna be using dash (-) which will indicate it is an item in the list.

  # Lists in YAML

  hobbies:
    - movies
    - riding bike

  # Another notation 
  movies: ["A Beautiful Mind", "The Pursuit of Happyness", "Dark Knight"]

  # Complex List like List of Objects
  # Each entry in this list will be an object
  friends: 
    # Way 1
    - name: "Udit"
      age: 25

    # Way 2
    - {name: "Sagar", age: 24}

    # Way 3 : Easier to read
    - 
      name: "Gajendra"
      age: 25

Multiline & Formatted Information

  • We can store information in multiple line which in easy to read and considered as a single line.

  • We can also preserve the format of the information.

# Multiple Lines & Formatted Information

# Writing Information in mupliple lines for readability but as a single line

  # Consider as a single line
  bio: >
    I am Software Developer
    from India,
    and I like tea.

  # Preserves the formatting of the data
  signature: |
    Sanyasee Sahu
    Software Developer
    Email- sanyasee12@gmail.com

Anchoring

  • Anchor a value and access it in other places in the yaml file.

  • An anchor does not have to match the key.

  • We can define an anchor by (&) and access it by (*).

  • We can also anchor an entire key-value pair.

# Anchoring
person: 
  firstName: &name "Sanyasee"
  lastName: 'Sahu'
  occupation: Software Developer

  id: *name

  # Anchoring enitre key-value pair
  base: &base
    var1: value1

  foo:
    <<: *base       # var1: value1
    var2: value2

Other Data Serialization Languages

  • Let's take a same piece of information and represent it using YAML, JSON & XML.

YAML

Peoples: 
  - name: "Sanyasee Sahu"
    age: 24
    occupation: "Software Developer"
    company: "HashedIn by Deloitte"

JSON

{
    "Peoples": [
        {
            "name": "Sanyasee Sahu",
            "age": 24,
            "occupation": "Software Developer",
            "company": "HashedIn by Deloitte"
        }
    ]
}

XML

<Peoples>
    <name>Sanyasee Sahu</name>
    <age>24</age>
    <occupation>Software Developer</occupation>
    <company>HashedIn by Deloitte</company>
</Peoples>

This is my first blog. Thanks for reading it, Hope you liked it.