Javatpoint Logo
Javatpoint Logo

YAML Data types

YAML has three types of data types:

  1. Scalar
  2. List
  3. Dictionary

Scalar data type:

Scalar is a simple data type. In YAML, scalar means a simple value for a key. The value of the scalar can be integer, float, Boolean, and string. Scalar data types are classified into two data types:

  1. Numeric Data type
  2. String

Numeric Data type

There are three types of numeric data type:

  • Integer
  • Floating point numbers
  • Booleans

An Integer data type can be decimal, octal, or hexadecimal.

For example:

Here, the hex value is indicated by 0x, and octal value is indicated by leading zero. When we run this document on our python script, the following output will be generated:

The floating-point value can be fixed and exponential.

For example:

When we evaluate the above entity, we will get the following:

A Boolean value can be True/False or Yes/No or On/Off.

For example:

String

YAML strings are Unicode. In the following example, we are going to define a simple string, without using quotes.

Example:

When we process this, the following output will be generated:

Now, we will define a string with an escape sequence. The following string contains a special character (anything other than alphanumeric), so it contains double-quotes.

When we process this, the following output will be generated:

During the YAML file, we can set the value of a data variable to be null. Later, we can write a program to change the value of null to any other value.

Our program processes this as:

In YAML, we can write a multi-line string in a single line using > symbol. In this, a newline character(\n) will be ignored.

Example:

The above string will interpret without the new lines as follows:

In YAML, we can write multi-line string in a newline using | symbol. In this, the newline character(\n) will be included.

Example:

So we see the new lines where they are in the document as follows:

Lists

We can define the list in a single line as follows:

This style is known as block style. We can put the above list in multiple lines as follows:

This style is known as flow style. A list that contains complex objects needs multiple lines.

Any number of valid YAML values can contain by an array. But the value of a list can't be the same type.

Dictionaries

If we want to write a complex YAML file which holds the complex data structure, we will use dictionaries. It is a collection of key: value pairs and each of the key: value pairs can be nested with a lot of options.

Example 1:

In the above example, student is the first key, and john is the value. Hobbies are the second key, but it is nested, which means it contains a list of values. The value of the key can again be a key: value pair, which we will see in the next example.

Example 2:

The subjectDetails shows a key, and the value of this key is a list of key: value pairs. fatherName, motherName, and subjectName are the keys. Where subjectName key contains a list of key: value pairs and subject1 and subject2 are the keys for values 70 and 100.


Next TopicYAML Styles





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA