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

The > 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:

List

Lists are very important in YAML. An example of the list is given below. 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. 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 which is equivalent to:

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 Create another file named feed.yaml with the following content: Let’s start by reading the file details.yaml After running the file details.py, we get the following output

Writing YAML to Files in Python

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: We have an npm library available for Node.js called js-yaml. Let’s start by installing the module by We then use the js-yaml module in our file.

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.

Introduction to YAML for Beginners - 12Introduction to YAML for Beginners - 70Introduction to YAML for Beginners - 24Introduction to YAML for Beginners - 59Introduction to YAML for Beginners - 30Introduction to YAML for Beginners - 19