YAML in Breif

Trinad
4 min readMar 15, 2023

--

YAML (YAML Ain’t Markup Language) is a popular choice for configuration files and inter-system data sharing due to its human-readable data serialisation format. Given its simplicity and readability, YAML is increasingly replacing XML and JSON in popular usage.

Key-value pairs, lists, and hierarchical structures are all used to represent information in YAML. What follows is an examination of the various forms that such information can take.

alphabets and symbols
Photo by Towfiqu barbhuiya on Unsplash

Scalars

Scalars represent a single value, such as a string, number, or boolean. Here are some examples of scalar values in YAML:

# A string
name: John Smith

# An integer
age: 42

# A floating-point number
price: 3.99

# A boolean
is_enabled: true

Scalars can also be written as a block scalar, which allows for more complex formatting:

# A block scalar
description: |
This is a block scalar that allows for
multiple lines of text and formatting.

Lists

Lists represent a collection of values, and are denoted by a hyphen (-) followed by a space. Here is an example of a list in YAML:

# A list of strings
fruits:
- apple
- banana
- orange

Lists can also contain nested data structures, such as maps:

# A list of maps
people:
- name: John Smith
age: 42
- name: Jane Doe
age: 35

Maps

Maps are collections of key-value pairs that can alternatively be referred to as dictionaries or associative arrays. A map is indicated by a colon (:) followed by a space, and each key-value pair is separated by a newline. A sample YAML map is shown below:

# A map of string keys and values
person:
name: John Smith
age: 42

Maps can also contain nested data structures, such as lists:

# A map of list values
fruits:
- name: apple
color: red
- name: banana
color: yellow
- name: orange
color: orange

Null

In YAML, null represents the absence of a value. Null is denoted by a tilde (~). Here is an example of null in YAML:

# A null value
favorite_color: ~

Anchors and Aliases

Anchors and aliases allow you to reuse data in a YAML document. An anchor is denoted by an ampersand (&) followed by a name, and an alias is denoted by an asterisk (*) followed by the same name. Here is an example of anchors and aliases in YAML:

# A map with an anchor
person: &person
name: John Smith
age: 42

# An alias to the anchor
friend: *person

Multi-line Strings

YAML allows you to create multi-line strings by separating them with | and >. When a block scalar is created using the | character, line breaks and formatting are kept in tact, while when a folded scalar is created using the > character, line breaks and leading/trailing whitespace are omitted. As an illustration, consider the following:

# A multi-line string using a block scalar
description: |
This is a long description
that spans multiple lines.
The block scalar preserves
the line breaks and formatting.

# A multi-line string using a folded scalar
summary: >
This is a summary that spans
multiple lines but is folded
into a single line by removing
line breaks and leading/trailing whitespace.

Comments

YAML allows for both inline and block comments. Inline comments are indicated by the # symbol and extend to the line’s end. You can identify a block comment by its signature format: # followed by a space, followed by a line with only a # character. Let’s look at an illustration:

# This is an inline comment

# This is a block comment
#
# It spans multiple lines
# and ends with a line
# containing only a # character.

Implicit and Explicit Typing

In YAML, you may use either implicit or explicit typing. When a value is encountered that “looks” like an integer, implicit typing will transform it to a genuine integer. During explicit typing, a data type is designated by a tag, signified by the! symbol followed by the tag name. As an illustration, consider the following:

# Implicit typing
age: 42 # Integer
price: 3.99 # Float
enabled: true # Boolean

# Explicit typing
pi: !float 3.14159
date: !timestamp '2022-03-15 10:30:00'

Inheritance and Overrides

YAML allows for inheritance and overrides of values using the << and ! characters. The << character allows a map to inherit key-value pairs from another map, while the ! character allows a value to be overridden by a later occurrence of the same key. Here’s an example:

# Inheritance using the << character
parent:
name: John
age: 42

child:
<<: *parent
name: Jane

# Overrides using the ! character
person:
name: John
age: 42

# Overrides the name key
name: !str Jane

Tags

YAML tags provide additional information about a value’s data type, such as specifying a custom class or format. Tags are denoted by a ! character followed by the tag name. Here are some examples of YAML tags:

# Custom class tag
person: !Person
name: John Smith
age: 42

# Format tag
date: !date '2022-03-15'

Conclusion

These are some additional details about YAML and its data types. YAML’s flexibility and human-readable format make it a popular choice for data exchange and configuration files in various programming languages and systems.

--

--

Trinad

A software developer with some background in creating SASS applications who is eager to hone his craft via constant practise.