October 1, 2020

Getting Started With YAML

YAML Tutorial To Get Started

Getting Started With YAML

This tutorial is aimed at beginners who want to get started with YAML.

What is YAML?

YAML stands for YAML Ain't Markup Language. This is not a regualar acronym and it's called recursive acronym. Like XML and JSON, YAML is one more data exchange formats.

  YAML is often used as part of configuration files. YAML follows python style indentations and is a preferred data format for Swagger Editor.

YAML Syntax and Basic Components:

1. Indentation

Unlike JSON and XML, YAML uses indentation for nested elements. Only whitespaces  are to be used. "Tabs" are not allowed.

For Eg: Let us consider JSON data:  

"firstName" : "Raja"
"lastName" : "Vikramarka"
"age" : 31
"address" : { "DoorNumber" : "310F",
"Building" : "Pink Sky Apartments",
"Street" : "Wilson Garden"}

The YAML data of above JSON looks like

firstName : Raja
lastName : Vikramarka
age : 31
address :
   DoorNumber : 310F
   Building : Pink Sky Apartments
   Street : Wilson Garden

We can observe the differences. There are no flower brackets "{", there are no quotes and the child objects of "address" field are presented on next lines using two whitespaces indentation.

For each inner nesting, we need to indent one more level. To understand, let us modify our JSON  a bit.

JSON Address

{"firstName": "Raja",
"lastName": "Vikramarka",
"age": 31,
"address": {"DoorNumber": "310F",
"Building": "Pink Sky Apartments",
"Street": "Wilson Garden"},
"parentsDetails": {"Mother": "Susheela",
"Father": "Ramachandra",
"address": {"DoorNumber": "300F",
"Building": "Blue Petal Apartments",
"Street": "Brigade Road"}}}

We introduced one more level of nesting. When we write it in YAML, the output looks like as shown below.

firstName: Raja
lastName: Vikramarka
age: 31
  DoorNumber: 310F
  Building: Pink Sky Apartments
  Street: Wilson Garden
  Mother: Susheela
  Father: Ramachandra
    DoorNumber: 300F
    Building: Blue Petal Apartments
    Street: Brigade Road


The hash or number key (#) is used for commenting single lines. There is no support for multiline comments.

#This is a YAML comment


To denote a list of elements, we use "-" dash/hyphen symbol  and a whitespace before each element in YAML. For eg:

We can use squarebrackets to indicate the list as well.


["Apples", "Oranges", "Bananas"] #This is correct as well


We have seen the data in YAML is usually a key value pair seperated by a colon(:). Key is always a string. However, the value can be of any datatype.

In YAML, Both double quotes and single quotes are allowed to indicate Strings.They are however optional.

name : Raj # This is correct
Alias : "Raja" # This is correct as well
Profession : 'Engineer' # This is correct as well

When we don't have a normal String, we have to use quotes. For eg:

version : "3.0"
sample : " This is a line with carriage return \n"

To use Number and Boolean data formats, we have to use like

Age :35 # This is a integer datatype. Should not use quotes here.

IsEmployed: False # This is a boolean datatype.

GPA: 7.8 # This is a floating point integer

5. Multiple Documents

To use multiple YAML documents in a single file, we have to use "- - -" .

For Eg:

Multiple YAML Docs

With this, we have completed the basic tutorial on YAML and you can start working on YAML file format and can learn new things in the process.