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)
—--
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
Now compare it to JSON format:
#JSON
{
"Imaro": {
"author": "Aldous Huxley",
"language": "English",
"publication-year": "1932",
"pages": 311
}
}
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
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
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
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
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
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
Single-line sequences look more concise but are less readable.
animals: [dog, cat, elephant]
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: ~
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
Top comments (25)
In another use cases, YAML is used for static sites and docs building tools like mkdocs and material mkdocs;
Through this language you can define the structure of any page or view. I share this use case because I used YAML for building a documentation site for an Desktop App.
Greetings from Mexico 🤠👌
Thanks for pointing that out, Andre!
Hi Maria, good introduction to a bunch of things that YAML can do. Have you checked out this article on some of the things that can go wrong with YAML though? It's important to know some of the issues YAML can cause as well as it's extensive feature-set.
I've read the article you're referring to; it's really awesome if you need more information about YAML and its challenges 👍🏻
Some people say YAML is Gammel, but not me. 😉
I completely agree 😁
I believe, that YAML can be a valuable skill to learn because of its widespread use in various fields such as software development, configuration management, and DevOps.
Awesome post, btw👍🏻
Great point, Amanda☺️
From my experience YAML it's "simple" and "human readable" only for a few lines of code... For large lines of configuration, is too messy and unreadable. I wouldn't recommend it for large configurations.
I understand what you mean. YAML (YAML Ain't Markup Language) is indeed designed to be simple and easy to read for human beings, but as the complexity of the configuration increases, it can become more difficult to read and maintain. For larger configurations, alternative data formats, such as JSON or XML, may be more suitable as they offer more structured and standardized ways of representing complex data.
However, YAML still has its advantages, such as its clean and concise syntax, which makes it easier to write and understand than other formats. Additionally, many popular software tools, such as Kubernetes and Ansible, use YAML as their configuration file format, which makes it widely used and well supported in the community.
Thanks, Maria for such an informative post! For me, the dest thing in YAML is simple and readable syntax
Completely agree!
Great post! I've question regarding YAML: How does YAML syntax compare to other data serialization formats in terms of simplicity and readability? Just to clarify)
YAML is known for its simple and human-readable syntax, making it a popular choice for data serialization. Compared to other formats like XML and JSON, YAML has a lower learning curve and requires less code to represent the same data, making it easier to read and write for developers. However, YAML does have some limitations in terms of data typing and structure compared to more robust formats like JSON and XML.
The structure of any page or view may be defined using this language, which is also used for static sites and docs creation tools like mkdocs and material mkdocs. I'm sharing this use example since I built a documentation website for a desktop app using YAML.
Sounds like a great experience!
Great article @mariamarsh
Thank you, Kumar☺️
What does this mean?
!!yes bool
The expression "!!yes bool" is a reference to a boolean value in programming. The double exclamation marks (!!) before "yes" indicate a forced conversion of the string "yes" to a boolean value. Since the string "yes" is a truthy value, this conversion would result in the value "true".
Altough I didn't understand the part of type section, overall, easy to understand and good explanation :) Thank you for sharing the good post :)
Intersting, and what about TOML? Could you check on this...
I believe YAML is more widely adopted and supported by a larger number of programming languages and tools, while TOML is a newer format with a smaller community of users and integrations. However, I'll consider it one of the future topics for my posts!
Good article, I enjoyed reading it! If you're interested, feel free to check out my articles as well
Hi thanks for sharing!!
I liked the text. It's direct and concise.