Understanding YAML
In this section, we will learn the different ways in which the YAML data is represented.

Data serialization

Whenever you want to send some data structure or an object across computer networks, say the Internet, you have to turn it into a special format to read it and store it. The process is commonly known as serialization and is of enormous importance on the web. A common usage example of serialization is when reading data from databases and transferring it across the web.

What is YAML?

YAML is a data serialization format that stands for YAML ain’t Markup language.
The main advantage of using YAML is readability and writability. If you have a configuration file that needs to be easier for humans to read, it’s better to use YAML. YAML is not a complete substitution of JSON as JSON and XML have their places too; nevertheless, it’s useful learning YAML.
Another benefit of YAML is its support of various data types like cases, arrays, dictionaries, lists, and scalars. It has good support for the most popular languages like JavaScript, Python, Ruby, Java, etc.
Rules for Creating YAML file
When you are creating a file in YAML, you should remember the following basic rules:
  • YAML is case sensitive
  • The files should have .yaml or .yml as the extension
  • YAML does not allow the use of tabs while creating YAML files; spaces are allowed instead

The structure of a YAML file

The file starts with three dashes. These dashes indicate the start of a new YAML document.
1
---
2
3
Copied!
The following are the building blocks of a YAML file:
  1. 1.
    Key Value Pair — The basic type of entry in a YAML file is of a key value pair. After the Key and colon there is a space and then the value.
  2. 2.
    Arrays/Lists — Lists would have a number of items listed under the name of the list. The elements of the list would start with a -. There can be a n of lists, however the indentation of various elements of the array matters a lot.
  3. 3.
    Dictionary/Map — A more complex type of YAML file would be a Dictionary and Map.

The importance of indentation and spaces

Consider the following diagram, which has details about “Banana.” There are 3 attributes:
  1. 1.
    Calories = 200
  2. 2.
    Fat = 0.5g
  3. 3.
    Carbs = 30g
Suppose there is an extra indentation or spaces used — then the whole meaning of the YAML object changes as you can see below. So we need to be careful with respect to indentation and spaces.

YAML Datatypes

Here are a few notes about YAML syntax:

Strings

1
# Strings don't require quotes:
2
title: Introduction to YAML
3
4
# But you can still use them:
5
title-with-quotes: 'Introduction to YAML'
6
7
# Multiline strings start with |
8
execute: |
9
npm ci
10
npm build
11
npm test
Copied!

Numbers

1
# Integers:
2
age: 35
3
4
# Float:
5
price: 18.99
Copied!

Boolean

1
# Boolean values can be written in different ways:
2
published: false
3
published: False
4
published: FALSE
Copied!

Null values

1
# Null can be represented by simply not setting a value:
2
null-value:
3
4
# Or more explicitly:
5
null-value: null
6
null-value: NULL
7
null-value: Null
Copied!

Dates & timestamps

1
# ISO-Formatted dates can be used
2
3
date: 2002-12-14
4
canonical: 2001-12-15T02:59:43.1Z
5
iso8601: 2001-12-14t21:59:43.10-05:00
6
spaced: 2001-12-14 21:59:43.10 -5
Copied!

Arrays / Lists

1
# A list of numbers using hyphens:
2
numbers:
3
- one
4
- two
5
- three
6
7
# The inline version
8
numbers: [ one, two, three ]
Copied!

Dictionaries

1
# An employee record
2
martin:
3
name: Martin D'vloper
4
job: Developer
5
skill: Elite
6
7
# The inline version
8
martin: {name: Martin D'vloper, job: Developer, skill: Elite}
Copied!

Dictionaries vs Lists

Dictionary is an unordered collection whereas lists are ordered collection.

Nested values

We can use all of the above types to create an object with nested values, such as lists of dictionaries, dictionaries whose values are lists or a mix of both:
1
# Employee records
2
- martin:
3
name: Lisa
4
job: Developer
5
skills:
6
- python
7
- perl
8
- pascal
9
- tabitha:
10
name: Linda
11
job: Developer
12
skills:
13
- lisp
14
- fortran
15
- erlang
Copied!

List of Objects

1
# Let's list books:
2
- nineteen-eighty-four:
3
author: George Orwell
4
published-at: 1949-06-08
5
page-count: 328
6
description: |
7
A Novel, often published as 1984, is a dystopian novel by English novelist George Orwell.
8
9
- the-hobbit:
10
author: J. R. R. Tolkien
11
published-at: 1937-09-21
12
page-count: 310
13
description: |
14
The Hobbit, or There and Back Again is a children's fantasy novel by English author J. R. R. Tolkien.
Copied!
Also remember any line beginning with a hash is automatically ignored and considered as a comment.
.
.
.
.
Last modified 5mo ago