Intro to YAML#1

Intro to YAML#1

By Vaishali Rawat

What the heck is Yaml?

YAML stands for YAML AIN'T MARKUP LANGUAGE. As the name already reads itself. YAML is ain't no markup language (unline HTML). It is not a programming language, it's a data format used to display and exchange data. It is similar to XML (Extended markup language) and JSON. In YAML, we can only store data not commands. And storing data in the form of files is called Data Serialization(explained below).

Why use YAML?

  • YAML is used to configure dockers and Kubernetes files.
  • Used in logs and caches.

Benefits💥

  • Simple and easy to read.
  • It has a strict syntax. Hence, indentation is very important.
  • Easily gets converted into JSON or XML.
  • Most languages across the world use YAML.
  • More powerful when representing Complex data.
  • Parsers and various other handy tools are also available.
  • YAML is case sensitive.

image

Data Serialization and De-Serialization

A process of converting this data object (code+data) into a series of bytes that saves the state of the object in a form that is easily transmittable.

Picture1-750x374.png

Datatypes

Key Datatype

YAML is written with the help of a key and then a specific value is provided to that key. It uses a key-value pair. Eg:

 "apple" : "I am a red fruit" `

 59: "This is vaish's roll number"

Here, "apple" is the key and hence its value is provided.

List

This one is pretty easy. Eg:

List Datatype: 

      -Apple
      -Mango 
      -Banana

Block Style

  • a way of formatting YAML documents that are typically used to represent hierarchical data structures.

  • In block style, each element of the document is completely contained within its own block element.

Eg:

---
users:
  - name: John Smith
    age: 25
    job: programmer
  - name: Jane Doe
    age: 30
    job: manager
...

In the above code,--- denotes the start of a new document, whereas ... denotes the end of the document.

YAML is basically a collection of 0 or more documents, seperated by ---.

Advanced Datatypes

Numeric Datatypes

  • Integer- number: 6786

  • Floating-point number- marks: 67.89

  • Bool- boolean value: True

  • Others-

    zero: !!int 0
    positiveNum: !!int 45
    negativeNum: !!int -99
    octalNum: !!int 07889
    hexa: !!int 0x45
    commavalue: !!int +540000 # 540,000
    

    Here, !!int is written to specify the data type used.

Sequence Datatype

In its simplest form, there's one item per line, with each line beginning in a dash and a space. Here's an example:

student: 
 -marks
 -name
 -roll_no

Sparse Sequence

Some of the keys of the sequence will be empty unlike name, class etc that requires a value to be stored in them.

eg:

sparse seq: 
 -hi
 -how 
 -null
 -sup

Anchors

Anchors and Aliases are YAML constructions that allow you to reduce repeat syntax and extend existing data nodes. You can place Anchors ( & ) on an entity to mark a multi-line section. You can then use an Alias ( * ) call that anchor later in the document to reference that section.

//putting an anchor here to avoid repetition in the below information about two people.
likings: &likes       // anchor
   fav fruit: mango
   dislikes: grapes

person1:
  name: vishaka
  <<: *likes       //alias

person2:
  name: aman
  fav fruit: mango
  dislikes: grapes

What now?

Half Knowledge is dangerous. So, play with the snippets on your own. And research what can be done with YAML, where it is used, and at what extent it is used.

Happy Learning, Fellas.