What Ain't A Markup Language? YAML

What Ain't A Markup Language? YAML

What is YAML?

  • YAML stands for Yet Another Markup Language. Now, we call it YAML Ain't Markup Language.

  • It's not a programming language, instead, it's a data format used to exchange data just like JSON and XML are used.

  • We use YAML for data storing operations.


Why would you need to use YAML Files?

  • YAML is a digestible data serialization language often used to create configuration files with any programming language.

  • Designed for human interaction

  • YAML is a strict superset of JSON, another data serialization language.

  • has strict syntax -> indentation is important

  • more powerful when using complex data


What is Data Serialization?

  • a process of converting data objects into a complex data structure/stream of bytes.

  • serialization helps the data to save or transmit into either YAML files, memory etc.

  • The reverse process—constructing a data structure or object from a series of bytes—is deserialization


Languages used for Data Serialization?

  • YAML

  • JSON

  • XML

These languages are used to represent data in form of code

Object -> File: Serialization

File -> Object: Deserialization


Getting into the coding side of YAML

  • The extension to make a YAML file is : .yml or .yaml

Datatypes in YAML

  • String variables:

  • These are some of the ways to create string variables in YAML

myself : Akash
fruit : "orange"
job : 'swe'
  • Integer, Float, Bool:
number : 54
marks : 56.6
booleanV : No(false)
booleanV : Yes(true)

Specifying the datatypes in YAML

'!!' are used with the data type you want to create

  • For integers:
zero : !!int 45
binaryNum : !!int 0b11001
commaValue : !!int + 540000
  • For floating values:
marks : !!float 56.89
  • Other data types:
infinite : !!float .inf
not a num : .nan
boolV : !!bool No
strig : !!str
exponential num : 6.023E56

Comments in YAML

  • '#' is used to create comments in YAML

  • It does not support Multi- line comments


Lists in YAML

There are multiple ways to create lists in YAML. Some are given below

-apple
-mango
-banana
cities : [newdelhi , mumbai , gujrat]
  • YAML is a collection of documents.

  • Documents are separated by '---':

-apple
-mango
-banana
---
-hero
-heroine
-model
---
  • To end a document, '...' are used
-apple
-mango
-banana
---
-hero
-heroine
-model
...

Advance Data types in YAML

  • We write the list as a sequence in YAML
students : !!seq
-marks
-name
-rollno

Note: There might be a case when some keys are empty. Then the sequence is called a "Sparse Sequence".

sparse seq:
-marks
-name
-
-rollno

Nested Sequence

-
    -mango                               #nested items
    -apple                               #nested items 
-

Key-Value pair or Maps

  • The syntax to create such maps are:
name : Akash
age : 15

Nested Map

name : Akash
role : 
        age : 15
        email : "aka@yahoo.com"

Reusing the properties using Anchors

  • Reusing the properties is the same as using a bunch of codes in different places without just copying and pasting the same code again and again
likings : &likes
    fav fruit : mango
    dislikes : grapes

# to copy the properties above to another map

person1 : 
    name : Akash
    << : *likes
    # you can overwrite too 
    dislikes : berries

Reference

Kunal Kushwaha

I appreciate your reading, buddy. Please like, comment on, and share this post if you find it beneficial.

Connect with me on :


Did you find this article valuable?

Support WeMakeDevs by becoming a sponsor. Any amount is appreciated!