If you are a developer writing APIs, you've probably come across YAML even though your primary serialization tool might be JSON.

YAML has its own friendly syntax, and it's a handy language to add to your development arsenal.

Let's learn the basics of YAML.

Data serialization

Whenever you want to send some data structure or an object across computer networks, say the Internet, you have to turn it into a special format to read it and store it. The process is commonly known as serialization and is of enormous importance on the web. A common usage example of serialization is when reading data from databases and transferring it across the web.

Some serialization formations include JSON, YAML, XML.

In this article, we talk about YAML, and at the end of the article, you'll be able to work your way through YAMLand have a clear introduction to YAML.

What is YAML, and how does it benefit me?

YAML is a data serialization format that stands for YAML ain't Markup language.

The main advantage of using YAML is readability and writability. If you have a configuration file that needs to be easier for humans to read, it's better to use YAML. YAML is not a complete substitution of JSON as JSON and XML have their places too; nevertheless, it's useful learning YAML.

Another benefit of YAML is its support of various data types like cases, arrays, dictionaries, lists, and scalars. It has good support for the most popular languages like JavaScript, Python, Ruby, Java, etc.

YAML only supports spaces, and it is case sensitive as well as space sensitive. Tabs are not accepted universally. A YAML file has .yaml extension.

Basic YAML syntax

Every YAML starts with --- which denotes the start of a YAML file.

When creating an API, we're interested in a feature provided by YAML known as mapping.

The following examples show the example of mapping in YAML.

---
name: James
boy: yes
GPA: 3.41

The mapping syntax is key: value. (Note the space, it's very crucial in YAML, unlike JSON or XML.

YAML also supports data types like characters, strings, integers, floating values, and collections like arrays, lists which are constructed from basic data types.

Data Types in YAML

Let’s see the example of a YAML below:

---

MALE: FALSE

GPA: 3.61

ISSUES: NULL

NAME: “BIGYAN”

AGE: 16

The first data type is a boolean, where it can have two values: true or false. The value of GPA is floating-point. YAML also supports the null data type as we have for “Issues”. The value of “Name” is a string that needs to be quoted inside double or single quotes. YAML also supports multiline string and multiple line string as a single for readability.

Multiline and single-line strings

---

About: >

 Hello this is Ryan

 From Alabama and I like to

 Play soccer.

The <i>></i> symbol lets you write a single line string to multiple lines. The sentence is actually a single line description though we have multiple lines.

We can also have multiple line strings if we use the | symbol like allowed:

About: |

 This is a multiline string

 And will be printed line wise.

List

Lists are very important in YAML.

An example of the list is given below.

---

- apple

- banana

- mango

Mapping from scalar to lists is shown below, which is very important for most configuration files.

---

Fruits:

 Apples

 Banana

 Guava 

Nesting is required for mapping scalar to list. We can also have multiple nested lists, as shown in the example below.

Automobiles:

 Car:

     Hyundai

     Volkswagen

     Ford

Here cars are nested inside automobiles, and Hyundai is nested inside cars. This is an example of multiple nesting. We can have multiple nesting as much as we want.

Subjects:

     Engineering:

       Mechanical engineering:

         Design and manufacture

         Automobile

         Control and Design

       Civil engineering:

         Structural engineering

         Hydropower

       Arts:

         Medieval

         Modern

         Painting

YAML also provides & and * symbols as anchors and references to the anchor to avoid duplication. They are essential in configuration files in frameworks like Ruby on Rails to make the YAML file smaller.

See the example below

<span class="hljs-attr">details:</span> <span class="hljs-meta">&details</span>
    <span class="hljs-attr">name:</span> <span class="hljs-string">"John"
</span>    age: 18
profession: engineer

<< : * details

which is equivalent to:

profession: engineer

name: "John"

age: 18

YAML in Python

Python supports YAML, including some modules like ruamel and pyyaml. Start by installing pyyaml

pip install pyyaml

For the tutorial, create a file with a name details.yaml

name: "john"

age:18

gender: male

Create another file named feed.yaml with the following content:

sports:

 football
 basketball
 cricket
 baseball

---
countries:
 Brazil
 Lithuania
 Australia
 USA

Let's start by reading the file details.yaml

import yaml

with open('details.yaml') as f:
    
    data = yaml.load(f, Loader=yaml.FullLoader)
    print(data)

After running the file details.py, we get the following output

 $ python details.py
{'name': "john", 'age': 18, 'gender': male}
import yaml

with open(r'feed.yaml') as file:
    # The FullLoader parameter handles the conversion from YAML
    # scalar values to Python the dictionary format
    fruits_list = yaml.load(file, Loader=yaml.FullLoader)

    print(fruits_list)

Writing YAML to Files in Python

import yaml

dict_file = [{'sports' : ['hockey', 'rugby', 'tennis', 'ping pong', 'football', 'badminton']},
{'countries' : ['Jamaica', 'England', 'Nepal', 'Netherlands', 'South Africa', 'Bolivia', 'Portugal']}]

with open(r'E:\data.yaml', 'w') as file: #create a new yaml file 
    data = yaml.dump(dict_file, file)

YAML implementation in Node.js

Node.js is a server-side processing language, and data serialization is of enormous importance in the development process.

For our tutorial, let's consider the following file example.yaml:

name:John

age:18

Hobbies:

 Hobby1:Football

 Hobby2:BasketBall

 Hobby3:Hockey

Job:

-System administrator

-Programmer

We have an npm library available for Node.js called js-yaml. Let's start by installing the module by

npm install js-yaml

We then use the js-yaml module in our file.

const yaml = require('js-yaml'); //initialize js-yaml
const fs   = require('fs'); //initialize filestream

try {
  const result = yaml.load(fs.readFileSync('example.yml', 'utf8'));
  console.log(result);
} catch (e) {
  console.log(e); //catch exception
}

Conclusion

In modern programming frameworks and applications where data is stored or distributed, YAML is becoming increasingly common in configuration files. YAML targets many of the same communication applications as Extensible Markup Language (XML) but has a minimal syntax that is deliberately different from XML.

YAML files can be created for fixed data structures using print commands that write both the data and the YAML's particular decoration. However, a dedicated YAML emitter is preferable for dumping various, or complex, hierarchical data. Similarly, with regular expressions, basic YAML files (e.g., key-value pairs) are readily parsed.