Beginners Guide to YAML
Tags: yaml, beginners, programming, webdev
Published: 2023-02-14 14:25:29

**The data serialisation format YAML(short for "YAML Ain't Markup Language")** is used to store and exchange data between various systems and programming languages. YAML is a good option for data that is intended to be read by people because it is made to be easily readable by humans.

In this article, I'd like to look at YAML's fundamental characteristics and the factors that contribute to its widespread use in DevOps tools.

---

**What is YAML?**

In the early stages of its development, YAML was known as "Another Markup Language," hence its name. Later, the phrase was changed to "YAML Ain't Markup Language" in order to separate it from legitimate markup languages and prevent misunderstanding.

The language has characteristics that are comparable to those of XML and JSON but employs a simpler syntax. When working with containers or writing configuration files for Infrastructure-as-Code (IAC) projects, YAML is frequently employed.

The majority of the time, YAML is used to build automation protocols that can carry out lists of commands that have been written in YAML files. Your system can become more independent and responsive as a result, without the need for additional developer attention.

The majority of the time, YAML is used to build automation protocols that can carry out lists of commands that have been written in YAML files. Your system can become more independent and responsive as a result, without the need for additional developer attention.

DevOps and virtualization are being used by an increasing number of businesses, therefore YAML is essential for today's developers. The PyYAML library, Docker, or Ansible, as well as other well-known technologies, make it simple to integrate YAML with support for Python and other popular technologies.

Let's now examine the key distinctions between YAML, JSON, and XML.

**YAML vs. JSON vs. XML**

I'll start by outlining some fundamental aspects of the YAML language that, in my opinion, set it apart from the competition:

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

I had also discussed relative anchors, extensible data types, and type mapping with key order retention as additional features.

Data-intensive apps that leverage virtual machines or DevOps pipelines are best suited for YAML. Additionally, enhancing data readability is advantageous in teams where developers and other team members work together often.

For instance, XML can be described as follows in comparison to YAML:

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

XML is generally the best choice for complex projects that call for precise control over namespace, schema, and validation. Although the language is difficult to read and uses more bandwidth and storage space, it offers unprecedented control.

As for JSON, it has the following advantages over YAML:

- 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 the ideal format for serialising and transferring data over an HTTP connection, and it is typically used in web development.

After going over the distinctions between the aforementioned three languages, I'd like to highlight some of YAML's most outstanding qualities that many developers adore.

**Characteristics of YAML**

**Various Document Support** One YAML file can include several YAML documents, making it simpler to organise files and parse data.

There are three dashes (---) between each document:

```

---
player: playerOne
action: attack (miss)
---
player: playerTwo
action: attack (hit)
—--

```
**Simple to read syntax** Python-like indentation rules are used in YAML files' syntax. To minimise confusion, spaces must be used instead of tabs. This eliminates unnecessary characters seen in XML and JSON (quotes, brackets, and curly braces). As a result, the file's readability is significantly improved:

```
#YAML
 Imaro:
 author: Rahul Bagal
 language: English
 publication-year: 2022
 pages: 212

```
Now compare it to JSON format:

```
#JSON 
{
   "Imaro": {
      "author": "Rahul Baga",
      "language": "English",
      "publication-year": "2022",
      "pages": 212
   }
}

```
**Incomplete executables** There are no executable files in YAML. As a result, sending YAML files to a third party is secure. YAML must be integrated with other languages, such Perl or Java, in order to employ executables.

**Comment assistance** Like Python, YAML allows comments to be added after the # symbol:

```
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 

```
**Syntax of YAML**

**Keys and values** Similar to a Python dictionary, YAML represents data using **key-value pairs**. For instance

```
key1: value1
key2: value2
key3: value3
```
**Strings** A word or a sentence can be contained within a "string," which is a group of characters. For single lines, use |, and for paragraphs, use >. In YAML, quotes are not required.

```
str: Hello World
data: |
    This
    Separate
    Strings
data: >
    This
    one paragraph
    text
```
**Dictionaries** Key-value pairs can be used to represent dictionaries in YAML. You may organise data into logical categories using dictionaries. For instance:

```
key1: value1
key2:
  key3: value3
  key4: value4
```

Dictionaries allow you to store intricate relational data since they can contain more sophisticated structures.

**Sequences** Sequences are data structures that store many values under the same key and resemble lists or arrays. When defining them, use indentation or [].

```
animals:
- dog
- cat
- elephant
```

While less readable, single-line sequences appear more condensed.

```
animals: [dog, cat, elephant]  
```
**Maps and scalars** A scalar, as the name suggests, is a single value. Standard types like int, float, boolean, string, and null are supported by YAML. They can be portrayed in hexadecimal, octal, or exponential form. Additionally, there are specific types for mathematical concepts like 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 is YAML capable of?**

In addition to the characteristics listed above, YAML also has:

* Anchors 
* Templates
*  Interaction with Docker, Ansible, etc. 
* Extended sequences and mapping
 * Extended data types (timestamp, null, etc.)

Since YAML syntax is intended to be straightforward and simple to use, it is frequently used for data that is meant to be read by humans. Key-value pairs and indentation allow YAML to represent a variety of data types.

The readability of YAML is one of its key advantages. YAML aims to be as clear and understandable as possible while yet being succinct. This makes it an excellent option for information that is meant to be accessed by others, such configuration files.

The integration of YAML with other well-liked DevOps tools, like Ansible, Docker, and Kubernetes, is another advantage. Anyone working in these fields would benefit from knowing YAML because it is used by these programmes to define configuration files.

Since YAML syntax is intended to be straightforward and simple to use, it is frequently used for data that is meant to be read by humans. Key-value pairs and indentation allow YAML to represent a variety of data types.

The readability of YAML is one of its key advantages. YAML aims to be as clear and understandable as possible while yet being succinct. This makes it an excellent option for information that is meant to be accessed by others, such configuration files.

The integration of YAML with other well-liked DevOps tools, like Ansible, Docker, and Kubernetes, is another advantage. Anyone working in these fields would benefit from knowing YAML because it is used by these programmes to define configuration files.

---

**In conclusion,** YAML is a versatile and easy-to-read data serialization language that can be used in a variety of applications. Its simple syntax makes it a great choice for configuration files, data exchange between programs, and more. By following the tips and best practices outlined in this guide, you can make the most of YAML and streamline your workflow.

If you found this guide helpful, don't hesitate to follow me on **[Twitter ](https://twitter.com/RahulBagal3002)**for more technology tips and insights. Thank you for reading!