What is YAML?

YAML stands for “YAML ain’t markup language”

YAML is a data serialization language designed to be used and understood easily by both programs and humans. It uses lines and spaces (no tabs) to define elements instead of {} or </>. Its syntax is similar to Python programming language.

YAML is a superset of JSON which means we can parse JSON with a YAML parser.

Here are some advantages of YAML:

  1. YAML is easy to read.
  2. YAML is designed to support serialization of any kind of data structure.
  3. YAML can refer other items within a YAML file with help of anchors. Hence, it can handle relational information as well.
  4. YAML is best suited for configuration files.
  5. YAML files can have comments.

Here is an example that displays data in XML, JSON and YAML form.

XML Form

<students>
  <student>
    <id>1</id>
    <name>Prisha</name>
    <age>3</age>
  </student>
  <student>
    <id>2</id>
    <name>Tanishka</name>
    <age>5</age>
  </student>
</students>

JSON Form

{
  "students": {
    "student": [
    {
       "id": "1",
       "name": "Prisha",
       "age": "3"
    },
    {
       "id": "2",
       "name": "Tanishka",
       "age": "5"
    }
    ]
  }
}

YAML Form

---
students:
  student:
    - id: '1'
      name: Prisha
      age: '3'
    - id: '2'
      name: Tanishka
      age: '5'

YAML Rules

  1. YAML is case sensitive.
  2. YAML file ends with .yml extension.
  3. YAML does not allow tabs.

Data Types

Basic: These are simple numbers and strings. For example:

name: "Ajay"
age: 41

Sequence/Array/List:
Represents collection of data. For example:

teams:
  - India
  - Australia
  - England

The above YAML is similar to:

teams = ['India', 'Australia', 'England']

Name of a list have syntax x: whereas elements of the list starts with “- ” at same spacing level. Sequences can be nested as well. Here is an example:

data:
  - - a
    - b 
  - - x
    - y

The above YAML is similar to:

data = [ ['a', 'b'], ['x', 'y'] ]

Hashes:
Hashes represent array of objects. First element of each object starts with – sign. For example, the data below represents two objects:

products:
 - sku         : BL394D
   quantity    : 4
   description : Basketball
   price       : 450.00
 - sku         : BL4438H
   quantity    : 1
   description : Super Hoop
   price       : 2392.00

The equivalent JSON for above YAML would be:

{
   "products": [
   {
      "sku": "BL394D",
      "quantity": 4,
      "description": "Basketball",
      "price": 450.0
   },
   {
      "sku": "BL4438H",
      "quantity": 1,
      "description": "Super Hoop",
      "price": 2392.0
   }
 ]
}

Maps:
A map represents named objects. For example:

product:
  sku : BL394D
  quantity : 4
  description : Basketball
  price : 450.00

The equivalent JSON for above YAML would be:

{
   "product": {
      "sku": "BL394D",
      "quantity": 4,
      "description": "Basketball",
      "price": 450.0
   }
}

Here is one more example:

YAML:

Dev:
  age: 7
  gender: M
Tanishka:
  age: 9
  gender: F

JSON:

{
  "Dev": {
    "age": 22,
    "gender": "M"
  },
  "Tanishka": {
    "age": 24,
    "gender": "F"
  }
}

Merging Data

We can refer or merge a list or map into another one. Here are some examples:

Create a named unique list &name. Now refer this list anywhere with *name

Refer List

favourite_cities: &cities
  - Pune
  - Shimla

recommended_cities: *cities

The contents of favourite_cities will be copied to recommended_cities

JSON equivalent of above YAML would be:

{
  "favourite_cities": [
    "Pune",
    "Shimla"
  ],
  "recommended_cities": [
    "Pune",
    "Shimla"
  ]
}

Merge Map

defaults: &defaults
  error_log: error.log
  host: somewebsite.com
  mail:
    sender_name: your_name
    smtp_server: your_server
    login: your_login
    password: your_password
development:
  <<: *defaults
  host: devwebsite.com
production:
  <<: *defaults
  host: productionwebsite.com

Here, defaults map is merged into development and production by using << operator. The resultant output looks like this:

defaults: &defaults
  error_log: error.log
  host: somewebsite.com
  mail:
    sender_name: your_name
    smtp_server: your_server
    login: your_login
    password: your_password
development:
  error_log: error.log
  host: devwebsite.com
  mail:
    sender_name: your_name
    smtp_server: your_server
    login: your_login
    password: your_password
production: 
  error_log: error.log 
  host: productionwebsite.com
  mail:
    sender_name: your_name
    smtp_server: your_server
    login: your_login
    password: your_password

The JSON equivalent of above YAML would be:

{
  "development": {
    "error_log": "error.log",
    "mail": {
      "sender_name": "your_name",
      "login": "your_login",
      "password": "your_password",
      "smtp_server": "your_server"
    },
    "host": "devwebsite.com"
  },
  "production": {
    "error_log": "error.log",
    "mail": {
      "sender_name": "your_name",
      "login": "your_login",
      "password": "your_password",
      "smtp_server": "your_server"
    },
    "host": "productionwebsite.com"
  },
  "defaults": {
    "error_log": "error.log",
    "mail": {
      "sender_name": "your_name",
      "login": "your_login",
      "password": "your_password",
      "smtp_server": "your_server"
    },
    "host": "somewebsite.com"
  }
}