Introduction to YAML

Introduction to YAML

YAML

YAML means Yaml ain't markup language

Markup language is a type of computer language that uses clearly recognizable keywords, names, or tags to structure the appearance of a page as a whole and the data it contains. Some examples of a markup language are HTML, XML ..etc.

Why Yaml is called YAML ain't markup language? As we read above, markup languages store well-structured documents, but YAML also saves documents and objects. It is considered not a markup language (ain't markup language) for this reason.

Although it is a kind of computer language, YAML is not a programming language. In essence, it is a data format that is used for data communication. A rigid syntax defines it. Like XML and JSON, it is comparable. However, commands cannot be stored in YAML.

Serialization & De-Serialization

Assume for the moment that you have some data recorded in your YAML file and wish to share it with your peers. Everyone utilizes a distinct version, such as one for Android, Mac, Windows, etc. Do you realize it will be challenging to provide the data for each in accordance with their version format? Therefore, in order to resolve this issue, we must find a mechanism to exchange the data in a single format.

Serialization & De-Serialization helps to solve this problem. The process of turning data items into a stream of bytes so they may be saved or sent more quickly is known as Serialization. Deserialization is the process of creating a data structure or object from a collection of bytes.

Object is the combination of code + data. It is basically a data storage unit.

serialize-deserialize-java.png

Both the .yaml and the .yml extensions are acceptable for YAML files. Data may be saved in YAML files in a variety of ways. Few of them will be discussed in this blog.

Key - Value Pair

 "name": "Bhargav krishna"
1: "Favourite number"

We refer to it as Key to the left of the colon(:), and Value to the right of the colon(:). The key and value in the above key-value combination are "name" and "Bhargav Krishna," respectively.

The data type of the value need not be stated in this case. Data types are recognized automatically. However, if you like to mention the datatype, we can do it in the following manner

name: !!str Bhargav krishna   #string
Favourite number: !!int 1   #int
marks: !!float 35.5   #float
infinite: !!float .inf   #if the value is .inf then it is known as infinite if it is .nan then it is not a number
BoolanValue: !!bool No   #boolean
text: !!null
exponential numbers: 6.023E23

Comments

 "name": "Bhargav krishna"
#comments
1: "Favourite number"

We use # to represent comments in the YAML file.

Lists

# Lists
- apple
- Apple
- mango
- Banana

This is how we represent Lists.

Object

You can create objects in YAML by intending every key-value pair and enclosing it in an object. The space has to be the same for each attribute in the object.

{->} represents space.

cities:  # this is object 
->city1: Chennai  # these are attributes 
->city2: Hyderabad
->distance: 700

List of Objects

Here micro is the object and app is the list.

micro:
 - app: user
   port:500
   version:1.7
 - app: shoppingcart
   port: 600
   version: 1.9

From the below image you will get a clear idea about objects and a list of objects.

Screenshot (286).png

Multi-line String

If you want to write a multiline text in YAML you should use " | ".

multiline: |
 My name is bhargav Krishna 
 2nd-year undergrad 
 From India.

For interpretation of a single-line text, we use " \>.

singleline: >
 My name is bhargav Krishna 
 2nd-year undergrad 
 From India.

Maps

Key: value pairs are called maps.

t-shirt-size:
  complex-mapping:
    XS:
      uk: 6
      fr: 34
      us: 2
    S:
      uk: 8
      fr: 36
      us: 4
    M:
      uk: 10
      fr: 38
      us: 6
    L:
      uk: 12
      fr: 40
      us: 8

Different Documents

"name": "Bhargav krishna"
1: "Favourite number"

---

# Lists
- apple
- Apple
- mango
- Banana

---

cities:
 - delhi
 - Mumbai
 -chennai

...

** --- ** This differentiates the different documents which are in one single YAML file.

** ... ** means the document has ended.

That's all for now. Please like and share if you found this information informative. If you have any questions, please leave them in the comments section and I will do my best to answer them.

To read my other blogs, click here.

Thank you for reading, and I'll see you next time.