Geekflare is supported by our audience. We may earn affiliate commissions from buying links on this site.
In Development Last updated: January 11, 2021
Share on:
Invicti Web Application Security Scanner – the only solution that delivers automatic verification of vulnerabilities with Proof-Based Scanning™.

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:



GPA: 3.61



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.


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.






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






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.



       Mechanical engineering:

         Design and manufacture


         Control and Design

       Civil engineering:

         Structural engineering






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"


gender: male

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




Let’s start by reading the file details.yaml

import yaml

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

After running the file, we get the following output

 $ python
{'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)


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:








-System administrator


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'));
} catch (e) {
  console.log(e); //catch exception


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.

  • Bigyan Ghimire
Thanks to our Sponsors
More great readings on Development
Power Your Business
Some of the tools and services to help your business grow.
  • Invicti uses the Proof-Based Scanning™ to automatically verify the identified vulnerabilities and generate actionable results within just hours.
    Try Invicti
  • Web scraping, residential proxy, proxy manager, web unlocker, search engine crawler, and all you need to collect web data.
    Try Brightdata
  • is an all-in-one work OS to help you manage projects, tasks, work, sales, CRM, operations, workflows, and more.
    Try Monday
  • Intruder is an online vulnerability scanner that finds cyber security weaknesses in your infrastructure, to avoid costly data breaches.
    Try Intruder