DEV Community

Cover image for Quick guide for YAML
Maria 🍦 Marshmallow
Maria 🍦 Marshmallow

Posted on

Quick guide for YAML

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

Top comments (25)

Collapse
 
martydevs profile image
Andre Marti

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 🤠👌

Collapse
 
mariamarsh profile image
Maria 🍦 Marshmallow

Thanks for pointing that out, Andre!

Collapse
 
philnash profile image
Phil Nash

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.

Collapse
 
mariamarsh profile image
Maria 🍦 Marshmallow

I've read the article you're referring to; it's really awesome if you need more information about YAML and its challenges 👍🏻

Collapse
 
buzzdee profile image
Sebastian Schlatow • Edited

Some people say YAML is Gammel, but not me. 😉

Collapse
 
mariamarsh profile image
Maria 🍦 Marshmallow

I completely agree 😁

Collapse
 
amyswen451 profile image
Amanda Swensen

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👍🏻

Collapse
 
mariamarsh profile image
Maria 🍦 Marshmallow

Great point, Amanda☺️

Collapse
 
assunluis80 profile image
Luís Assunção • Edited

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.

Collapse
 
mariamarsh profile image
Maria 🍦 Marshmallow

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.

Collapse
 
phophogr profile image
Phoebe Green

Thanks, Maria for such an informative post! For me, the dest thing in YAML is simple and readable syntax

Collapse
 
mariamarsh profile image
Maria 🍦 Marshmallow

Completely agree!

Collapse
 
igibsonconor profile image
Conor Gibson

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)

Collapse
 
mariamarsh profile image
Maria 🍦 Marshmallow

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.

Collapse
 
zaidsab3 profile image
Zaid Sab

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.

Collapse
 
mariamarsh profile image
Maria 🍦 Marshmallow

Sounds like a great experience!

Collapse
 
kumarkalyan profile image
Kumar Kalyan

Great article @mariamarsh

Collapse
 
mariamarsh profile image
Maria 🍦 Marshmallow

Thank you, Kumar☺️

Collapse
 
jwp profile image
John Peters

What does this mean?
!!yes bool

Collapse
 
mariamarsh profile image
Maria 🍦 Marshmallow

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".

Collapse
 
lico profile image
SeongKuk Han

Altough I didn't understand the part of type section, overall, easy to understand and good explanation :) Thank you for sharing the good post :)

Collapse
 
vladttempest profile image
Alejandro Aguilera Wilches

Intersting, and what about TOML? Could you check on this...

Collapse
 
mariamarsh profile image
Maria 🍦 Marshmallow

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!

Collapse
 
arashrah profile image
Arash Rahimi

Good article, I enjoyed reading it! If you're interested, feel free to check out my articles as well

Collapse
 
bernardoow profile image
Bernardo Gomes

Hi thanks for sharing!!

I liked the text. It's direct and concise.