Quick guide for YAML

Maria 🍦 Marshmallow - Feb 10 '23 - - Dev Community

YAML (short for "YAML Ain't Markup Language") is a data serialization format that is used to store and exchange data between different systems and programming languages. YAML is designed to be easily readable by humans, making it a good choice for data that is intended to be read by people.

In this post, I'd like to take a look at YAML's basic features and the reasons why is it widely used in DevOps tools.

What is YAML?


At the beginning of YAML's development, it gained its name as an abbreviation for "Another Markup Language." Later on, creators decided to change that to "YAML Ain't Markup Language" to distinguish it from real markup languages and avoid confusion.

The language is similar to XML and JSON but uses a more minimalistic syntax while maintaining similar capabilities. YAML is commonly used to create configuration files in Infrastructure-as-Code (IAC) programs or to manage containers in DevOps work.

In most cases YAML is used to create automation protocols that can execute sequences of commands written in a YAML file. This allows your system to be more independent and responsive without additional developer attention.

More and more companies are using DevOps and virtualization, so YAML is a must-have for the modern developer. In addition, YAML is easy to integrate with support for Python (using the PyYAML library, Docker, or Ansible) and other popular technologies.

Now let's see what the main differences are between YAML, JSON, and XML.

YAML vs. JSON vs. XML

First, I will list some basic features of the YAML language that I believe distinguish it from the rest:

  • minimalistic syntax;
  • human-readable code;
  • cross-language compatibility;
  • inline style similar to JSON (YAML is its superset) and considered "cleaner" than JSON;
  • sharpened for working with data;
  • supports comments;
  • supports unquoted strings.

As additional features, I'd mentioned extensible data types, relative anchors, and type mapping with preservation of key order.

YAML is ideal for data-intensive applications that make use of DevOps pipelines or virtual machines. Furthermore, improving data readability is beneficial in teams where developers frequently interact with them.

Compared to YAML, XML, for example, can be described in the following ways:

  • more wordy;

  • harder to read;
  • acts as a markup language, and YAML as a data formatting language;

  • more features than YAML, such as tag attributes;

  • more rigid document schema.


In general, XML is ideal for complex projects that require fine control over validation, schema, and namespace. The language is poorly readable, requires more bandwidth and storage capacity, but provides unprecedented control.

As for JSON, compared to YAML, its features are these:

  • explicit, strict syntax requirements;

  • harder to read;

  • YAML-like inline style (some YAML parsers can read JSON files);

  • there have been no comments yet;
  • strings need double quotes.


JSON is normally used in web development, and it is the best format for serializing and passing data over an HTTP connection.

After we looked through the differences between the aforementioned three languages, I'd like to point out some of the most remarkable features of YAML that many developers love.

Distinctive features of YAML


Multi-Document Support
 You can combine multiple YAML documents into one YAML file for easier file organization and data parsing.

Documents are separated by three dashes (---):

---
player: playerOne
action: attack (miss)
---
player: playerTwo
action: attack (hit)
—--
Enter fullscreen mode Exit fullscreen mode

Easy-to-read syntax
 The syntax of YAML files uses an indentation system similar to Python. You must use spaces, not tabs, to avoid confusion.This gets rid of extra characters that are in JSON and XML (quotes, brackets, and curly braces). As a result, the readability of the file is greatly increased:

#YAML
 Imaro:
 author: Aldous Huxley
 language: English
 publication-year: 1932
 pages: 311
Enter fullscreen mode Exit fullscreen mode

Now compare it to JSON format:

#JSON 
{
   "Imaro": {
      "author": "Aldous Huxley",
      "language": "English",
      "publication-year": "1932",
      "pages": 311
   }
}
Enter fullscreen mode Exit fullscreen mode

Missing executables
 YAML does not contain executable files. Therefore, it is safe to exchange YAML files with a third party.To use executables, YAML needs to be integrated with other languages, such as Perl or Java.

Comment support YAML allows you to add comments after the # character, just like in Python:

key: #This is a single line comment
    - value line 2
    #This is
    #a multiline comment
  - value line 21
Enter fullscreen mode Exit fullscreen mode

Explicit and implicit typing YAML offers both autotype detection and the ability to explicitly specify the data type. To use a concrete type, write !![typeName] before the value.

# This value has been converted to an int:
14.10 is-an-int: !!int
# Converts any value to a string:
67.43 is-a-str: !!str
# Must be a boolean value:
is-a-bool: !!yes bool 
Enter fullscreen mode Exit fullscreen mode

YAML Syntax

Key-value pairs YAML uses key-value pairs to represent data, similar to a dictionary in Python. For example:

key1: value1
key2: value2
key3: value3
Enter fullscreen mode Exit fullscreen mode

Strings 
A "string" is a collection of characters that can contain a word or a sentence. You can use either | for single lines or > for paragraphs. Quotes are not needed in YAML.

str: Hello World
data: |
    This
    Separate
    Strings
data: >
    This
    one paragraph
    text
Enter fullscreen mode Exit fullscreen mode

Dictionaries YAML supports dictionaries, which can be represented using key-value pairs. Dictionaries allow you to divide data into logical categories. For example:

key1: value1
key2:
  key3: value3
  key4: value4
Enter fullscreen mode Exit fullscreen mode

Dictionaries can contain more complex structures, allowing you to store complex relational data.

Sequences Sequences are data structures similar to lists or arrays that store multiple values under the same key. They are defined using indentation or [].

animals:
- dog
- cat
- elephant
Enter fullscreen mode Exit fullscreen mode

Single-line sequences look more concise but are less readable.

animals: [dog, cat, elephant]  
Enter fullscreen mode Exit fullscreen mode

Scalars and mapping
 A scalar is a single value to which the name corresponds. YAML supports standard types: int and float, boolean, string, and null. They can be represented in different ways: hexadecimal, octal, or exponential. There are also special types for mathematical entities such as infinity, -infinity, and NAN.

integer: 25
hex: 0x12d4 #equal to 4820
octal: 023332 #equal to 9946
float: 25.0
exponent: 12.3015e+05 #equal to 1230150.0
boolean: Yes
string: "25"
infinity: .inf # converts to infinity
neginf: -.Inf #converts to minus infinity
not: .NAN #Not a Number
null: ~
Enter fullscreen mode Exit fullscreen mode

What else can YAML do?


Besides the features described above, YAML is also have:

  • Anchors

  • Templates
  • 
Interaction with Docker, Ansible, etc.

  • Extended sequences and mapping
  • 
Extended data types (timestamp, null, etc.)

YAML syntax is designed to be simple and easy to use, making it a popular choice for data that is intended to be read by humans. By using indentation and key-value pairs, YAML is able to represent a wide range of data types.

One of the main benefits of YAML is its readability. YAML is designed to be as concise as possible, while still being easy to understand. This makes it a good choice for data that is intended to be read by humans, such as configuration files.

Another benefit of YAML is its integration with many popular DevOps tools, such as Ansible, Docker, and Kubernetes. These tools use YAML to define configuration files, making it a valuable skill for anyone working in these areas.

Conclusion

In summary, YAML is a human-readable data serialization format that is used to store and exchange data between different systems and programming languages. It is widely used in DevOps tools, and is valued for its simplicity, readability, and flexibility.

I hope, that this post was useful for you to study YAML and it's capabilities! In which areas have you used YAML, and why do you think it can be a valuable skill to learn? Share your thoughts in comments!

P.S. In case you enjoy my posts and want to support me, I'll leave here a few links:)

Support me with Coinbase

Buy Me a Coffee at ko-fi.com

. . . . . . . . . . . . .
Terabox Video Player