What is YAML (Yet Another Markup Language)? | Learn YAML Basics
YAML (Yet Another Markup Language) Is Now (Yet Ain’t Markup Language)
About YAML:
- A data serialization language
- Easily readable and writable
- A strict superset of JSON
- Working well with all modern programming languages
- Portable between various programming languages
Declaration: this document is written with the consideration to use in Azure. Therefore there might be some conflicts or differences using it in other programming languages like python.
Main Rules:
- Indentation – YAML uses a fixed indentation scheme to represent relationships between data layers.
- Colons – Dictionaries simply follow key-value pairs. The left side of the colon is always a key and the right-hand side is always a value.
- Dashes – It represents the list of items. A single dash followed by the blank space is used for creating a list. YAML does not allow Tab.
- Case sensitive – YAML is strictly case sensitive
- File extension – YAML file extension could be .yaml or .yml
Understand the Basics of YAML syntaxes
- Use comments – The (#) symbol is used for writing the comments in the YAML file. If you want to write the paragraph, it is recommended to use the # at the beginning of each line.
Example:
###########################################################################
# Welcome to Azure Talk facebook group. This group will give you a great
# platform to share # what you know and ask what you want to know from the
# community members.
###########################################################################
There are mainly two types of blocks exist in YAML
- Scalar Type
- Structure Type
- Collection Type
Datatype
- Dictionaries – key: value pair
- String – with or without a single quote or double quote
- Number- inclusive real number, float number, exponential, hexadecimal, etc
- Boolean – True, False
- Null
- Array
YAML is pretty similar to JSON except it is more readable and strictly follows the indentation pattern. If you are familiar with JSON, then it is super simple for you to get into it.
Define variables/properties of different types with key: value pair
# define variables of different types using key: value pair pattern
name: "Azure Talk" # string, "" or '' both are accepted
group_members: 100 # number
rating: 4.5 # float
big_number: 1e+10 # exponential
when_created: 2020-05-05 # date
when_created_time: 2020-05-05 10:50:15 # date and time
open_community: true # boolean
Create an object having variable inside it
# define an object consists above variables inside it
community:
name: "Azure Talk"
group_members: 100
rating: 4.5
big_number: 1e+10
when_created: 2020-05-05
when_created_time: 2020-05-05 10:50:15
open_community: true
Note: community is an object and others (indented) variables are its properties which can be accessed using objects.properties notation like below
# how to access object properties?
community.group_members
community.rating
Create nested objects like list inside parent object
# Create list object inside a main object: way 1
---
community:
group_members: 120
moderators:
- "Dr Maitrey"
- "Mr Sharma"
- "Mr Kalia"
name: "Azure Talk"
Create a list object inside the main object: way 2
# Create list object inside a main object: way 2
---
community:
group_members: 120
moderators: ["Dr Maitrey","Mr Sharma","Mr Kalia"]
name: "Azure Talk"
Ignore multiline
Use (>) greater than the symbol before the text. The text written in multiple lines would be parsed or interpreted by YAML as a single line.
Example:
---
community:
group_members: 120
moderators: ["Dr Maitrey","Mr Sharma","Mr Kalia"]
name: "Azure Talk"
group_detail: > The Azure Talk group is an open community group to
extend the knowledge of the members. The core purposes
of this group is to share what we know and get what
you know.
The output will be in a single line:
The Azure Talk group is an open community group to extend the knowledge of …
Preserve multiline
---
community:
group_members: 120
moderators: ["Dr Maitrey","Mr Sharma","Mr Kalia"]
name: "Azure Talk"
group_detail: > The Azure Talk group is an open community group to
extend the knowledge of the members. The core purposes
of this group is to share what we know and get what
you know.
The output will be in multiple lines:
The Azure Talk group is an open community group to
extend the knowledge of the members. The core purposes
of this group is to share what we know and get what
you know.
Online YAML or YML validators:
https://onlineyamltools.com/validate-yaml