Understanding YAML and how to use them effectively

Hemanth Peela
AWS Tip
Published in
4 min readApr 28, 2022

--

YAML (Yet Another Markup language) to understand its definition and its usage, let me just give you an overview of why one needs YAML and before this existence what was used earlier instead of YAML files.

History

The markup language is widely used for the interchange of data over the Internet, earlier XML is very commonly used for storing and transmitting data using serialization. XML is mainly designed for documents where a wide variety of data is represented as <tags>.

XMLs are used for complex requirements inorder to interchange data.

During the course of time Webservices and Microservices emerged to rule over the web, Microservices gained popularity for their agility and for loose coupling.

JSON(Javascript Object Notation) grew out of a need for stateless to support microservice architecture, the dominant methods used in the early 2000s

JSON is promoted as a low-overhead alternative to XML as both of these formats have widespread support for the creation, reading, and decoding in the real-world situations where they are commonly used. Other widely used CSV file was an alternative but JSON won over the rat race for data interchange due to its human-readable format.

JSON is said to be slowly replacing XML because of easy to understand and need not worry about validation, every system can communicate without any limitations

JSON become one of the most popular and frequently used in most web applications, this is when the Cloud and Orchestration of services happen to kick in, and business wants to cut down the cost on CapEx/OpEx as well as meet the customer's demand.

JSON has a few limitations such as comments are not allowed, and formatting of JSON is difficult to read and for configuration in the applications, then Ruby and Perl started using YAML instead of JSON, which is a superset of JSON. YAML has indentation and allows comments which is a huge benefit for standard configurations.

YAML was Yet Another Markup Language but during the course of time it changed to YAML Ain’t Markup Language it is also a complete specification of the information needed to develop applications

YAML (YAML Ain’t Markup Language)

YAML is designed for data instead of documents. One of the most common uses for YAML is to create configuration files and is often used for data about Hosts and Infrastructure. It’s recommended that configuration files be written in YAML rather than JSON.

The YAML file is written in two ways: Block style and Flow style

There are building blocks that need to know:

  1. Sequences
  2. Mappings
  3. Scalars
  4. Comments
  5. Documents
Photo by Code project

learn more about YAML Syntax and building blocks examples here:

https://www.codeproject.com/Articles/1214409/Learn-YAML-in-five-minutes

https://www.educative.io/blog/yaml-tutorial

YAML Usage

YAML in DevOps:

It is used in Ansible, Kubernetes, Travis, Docker, etc. YAML build definitions can be added to a project by simply adding their source file to the root of the repository. To simplify the process of defining build and release tasks.

https://dzone.com/articles/popular-tools-supporting-yaml-data-format

YAML in Programming Languages:

Using programming languages like Python, Java, and .Net — YAML can be played in different ways:

  1. Can be converted into JSON data objects
  2. Transform YAML files into XML files
  3. Prettify and Minify YAML files
  4. URL encodings and Base encoding using YAML files
  5. APIs rest calls data validations using YAML files
  6. Converting YAML to Image files

There are a few use cases specific to some requirements, which can be explored in many different ways to play with the data.

YAML in Web development:

For creating any web application such as Angular/React/JS apps npm build is used. Node.js is responsible for any npm commands. Node.js is a server-side processing language, and data serialization is of enormous importance in the development process. npm library available for Node.js called js-yam

name:Hemanth

country:India

address:

city:Hyderabad
state:Telagana

dataSample.yml file has some data which can be used in the code.

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

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

By using this data in YML file can be used in any application-level configuration like Logger properties, Database connections, Security and Caching, etc.

Hope this blog about YAML gave you more insights into why YAML came into the picture and how it is used in projects.

Happy Reading!!!

Read more about YAML:

https://spacelift.io/blog/yaml

--

--

Hemanth Peela is an AVP at WellsFargo, an expert on Full Stack Development, Loves Travelling and Photography. Gadget Guru and Gaming Addict & Marvel Fanatic.