YAML Basic to Advance

Vikram Jadhav
Globant
Published in
6 min readAug 29, 2022

In this article, we will learn everything about YAML. As we all know Today in many tools YAML is used for configuration, so we should know all about YAML. We will start with a YAML introduction and then learn about all YAML concepts.

YAML

What is YAML?

  • YAML is a lightweight, human-readable data serialization language. It is designed to make the format easy to read and write. So, anyone can understand like a non-technical person can read and write in YAML.
  • YAML stands for “YAML Ain’t Markup Language”.
  • It is like XML and JSON but has easy and mini syntax.
  • YAML files are created with “.yaml” or “.yml” extensions. We can use any IDE (Integrated Development Environment) or text editor to open/create YAML files.
  • YAML is a superset of JSON.
  • It is quite easy, and we can represent the complex mapping in a straightforward way. Due to this nowadays YAML is used in configuration settings.

XML vs JSON vs YAML

Previously we used XML, then JSON came, and now YAML.

XML -> JOSN -> YAML

Examples of XML, JSON, and YAML

XML vs JSON vs YAML

Now we can check what the differences are between XML, JSON, and YAML and how it is easier and simpler than XML and JSON.

  • XML and JSON are harder to read but YAML is easy to read and understand.
  • For Hierarchy in XML, we use open and close tags. In JSON we use braces and brackets and in YAML we use double space characters.
  • XML requires huge storage and network bandwidths. JSON is lighter compared to XML and YAML is lighter compared to both.
  • XML is best for complex projects where control over schema is required. JSON is best for web development and data transfer over HTTP. YAML is best for configuration.
  • Comments are allowed in XML and YAML but not in JSON.

As YAML is easy to read and understand it is widely used in writing configuration files in different DevOps tools and cloud applications like YAML is used In Docker and Kubernetes.

Points to remember while writing the YAML files

YAML example
  • To denote the structure Indentation of whitespace is used.
  • A map is the basic structure of a YAML file, the same as a dictionary.
  • We cannot use tabs inside YAML, use white spaces.
  • YAML is case-sensitive.

Scalars

In YAML we write data in key-value pairs.

Scalars represent a single stored value. Scalars are assigned to key names as values. You define a key with a name, colon, and space, then a value for it to hold.

<key>: <value>

Example- name: Vikram

Note- space is important after the colon (“:”)

YAML supports common types like integer and floating-point numeric values, as well as non-numeric types of Boolean and String.

Example-

Scalar example

Strings

  • Strings in YAML do not need explicit double or single quotes.
  • Use single or double quotes in YAML if your value includes special characters. For example, these special characters may require quotes: {, }, [, ], ,, &, :, *, #, ?, |. -, <. >, =, !, %, @, \.
  • “Yes” and “No” should be enclosed in quotes (single or double) or else they will be interpreted as True and False Boolean values.

Folded style & Literal style in string-

  • use > (Folded style) to remove newlines within the string.
  • use | (Literal style) to turn every newline within the string into a literal newline.
  • You can control the handling of the final new line in the string, and any trailing blank lines (\n\n) by adding a block chomping indicator character
  • >, |: “clip”: keep the line feed and remove the trailing blank lines.
  • >-, |-: “strip”: remove the line feed, remove the trailing blank lines.
  • >+, |+: “keep”: keep the line feed, keep trailing blank EXAMPLE lines

Example-

Folded style & Literal style in string

Comments in YAML

  • We can add comments in the YAML file using the hash symbol (#)
  • YAML supports single-line comments but does not support multiline comments
  • Comments are skipped during the execution of YAML

Example-

# Comment in the YAML file

comments in YAML

Implicit and Explicit Type in YAML-

  • YAML understands the type implicitly when you assign value but if you want to define the type explicitly then also you can do that.
  • To define type explicitly use !![TypeName] before value.

Example-

Explicit type in YAML

Sequences

  • Sequences are values listed in a specific order. A sequence starts with a dash and a space (-).
  • We can write sequences in block style as well as flow style.
  • Block style uses spaces to structure the list.
  • In Flow styles, we can write sequences inline using square brackets.
  • We can write a sequence into another sequence.

Example-

Sequences in YAML

Dictionaries-

  • Dictionaries are collections of key-value pairs all nested under the same subgroup.
  • The key should be of type string only.
  • Dictionaries are defined with a name, a colon, and a space followed by 1 or more indented key-value pairs.
  • By using dictionaries, we create objects like Customer.

Example-

Dictionaries in YAML

Complex Keys:

  • We can define complex keys like multi-line keys in YAML.
  • Use ? followed by a space to indicate the start of a complex key.

Example-

Complex keys

Anchors and Alias-

  • If we want to repeat some part in our YAML file instead of writing this part multiple times in the YAML file, we can use Anchors and Alias to reduce the redundant code.
  • The repeated part we can define using Anchor ‘&’.
  • To refer to the Anchor we can use Alias ‘*’.
  • YAML anchors and aliases cannot contain the ‘ [ ‘, ‘ ] ‘, ‘ { ‘, ‘ } ‘, and ‘ , ‘ characters.

Example-

Anchors and Alias in YAML
  • We can override the Anchor values in the YAML by using ‘<<:’ before the alias.
Override Anchor value

Multi-Document in YAML-

  • In YAML the document starts with three dashes ( — -) and ends with three periods (…).
  • Some YAML processors require the document start operator. The end operator is usually optional.
  • We can have multiple YAML documents in a single YAML file. The separation between each document is marked by three dashes ( — -).
Document start and end

Security-

  • YAML is a User-Friendly Data Formatting Language. YAML does not contain any executable commands, which makes YAML highly secure when exchanging files with third parties.
  • If we require an executable command, then we need to integrate the YAML with other languages.

Conclusion:

YAML is a human-readable data-serialization language. We covered its commonly used features like Scalers, Sequences, Dictionaries, Complex keys, Anchors, and aliases. Due to its powerful language feature, YAML is used in Infrastructure as Code to write configuration files and data transfer. Docker uses YAML to create Dockerfile. In Kubernetes, we can create storage and virtual machines using YAML. Currently, YAML is widely used for pipeline development in DevOps.

--

--