What is YAML? A Begineer's Guide

What is YAML? A Begineer's Guide

YAML was previously known as "Yet Another Markup Language" but now it's called "YAML ain't markup language". 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. But because it’s a strict superset, it can do everything that JSON can and more. One major difference is that newlines and indentation actually mean something in YAML, as opposed to JSON, which uses brackets and braces

  • It is a Data serialization language similar to XML or JSON.

  • In YAML , You can stored data objects and it does not include command and instruction.

  • YAML does not allow the use of tabs while creating YAML files; spaces are allowed instead.

  • The files should have .yaml or .yml as the extension.

What is Data Serialization & De-Serialization?

Data serialization is the process of converting data object or object states that is present in complex data structures, into a stream of bytes for storage, transfer, and distribution in a form that can allow recovery of its original structure.

De-Serialization The reverse process of data serialization is known as Data De-serialization

serialization-deserialization-diagram-800x318-1.webp

YAML Character & Functionality

  • ? -> It denotes a mapping key
  • : -> It denotes a mapping value
  • _ -> It denotes a block sequence entry
  • [, ] -> Start and End of a flow sequence
  • {, } -> Starts and Ends a flow mapping
  • ! -> It denotes node's tag
  • | -> It denotes literal block scalar
  • -> It denotes a folded block scalar

  • % -> It denotes the directive use
  • --- -> this differentiates the different documents which are in one single YAML file.
  • ... -> means the document has ended

How To Write YAML

The basic structure of a YAML file is a map. You might call this a dictionary, hash or object, depending on your programming language or mood. Very generally, it’s keys and values all the way down:

  • Key-Value Pair
pair example: 
 - name: Yash
 - job: student

# same as
pair example: [name: Yash, job: student]

We refer to it as Key to the left of the colon(:), and Value to the right of the colon(:).

  • Array/List
Fruits:
- Orange
- Apple
- Banana

----

Vegetables:
- Carrot
- Cauliflower
- Tomato

This is how we represent Lists.

  • Dictionary/Map
Banana: 
    calories: 105
    fat: 0.4g
    carbs: 27g

---

Grapes:
    calories: 62
    fat: 0.3g
    carbs: 16g
  • Set
# !!set will allow you to have Unique value
names: !!set
  ? Yash
  ? Dhruv
  ? Rohit
  • String
#string: This is a comment
myself: "Yash" 
job: "Student"
favgame: "Volleyball"

bio: |
 Heyy myself Yash Pimple
 I am an  Introvert

#write a single line in multiple line 

message: >
  This will 
  be in single 
  line

#same as 

message: !!str This will be in single line
  • Numbers and boolean values:
number: 3564
marks: 95.68
booleanValue: !!bool No 
# n, N, false, False, FALSE
# same for true -> yes, y, Y
  • If you want to specify a specific type then :
# specify the type
zero: !!int 0
positiveNum: !!int 45
negativeNum: !!int -45
binaryNum: !!int 0b11001
octalNum: !!int 06574
hexa: !!int 0x45
commaValue: !!int +540_000 # 540,000
exponential numbers: 6.023E56
---
# floating point numbers
marks: !!float 56.89
infinite: !!float .inf
not a num: .nan
  • Let's Look at some advanced Data Types:

student: !!seq
 - marks
 - name
 - roll_no

# like this also
fruits: [banana, mango, apple]

# some of the keys of the seq will be empty
# sparse seq
sparse seq:
 - hey
 - how
 - 
 - Null
 - sup

# nested sequence
- 
 - mango
 - apple
 - banana
-
 - marks
 - roll num
 - date
  • Nested Mapping: Map within a map
name: Yash Pimple
role: 
   age: 18
   job: Student

#same as 

name: Yash Pimple
role: { age: 18, job: Student}
  • Key-value Pairs:
# pairs: keys may have duplicate values
# !!pairs

pair example: !!pairs
 - job: student
 - job: teacher

# same as
pair example: !!pairs [job: student, job: teacher]
# this will be an array of hashtables
  • Reusing properties with anchors
likings: &likes
  fav fruit: mango
  dislikes: grapes

person1:
  name: Yash 
  <<: *likes

person2:
  name: Dhruv
  <<: *likes
  dislikes: berries

# this will look like
person2:
  name: Nishant
  fav fruit: mango
  dislikes: berries

person3:
  name: Rohit 
  fav fruit: mango
  dislikes: grapes

Benefits of YAML :

  • Simple and easy to read

  • It has a strict Syntax due to which identation is important

  • Easily convertable to JSON , XML file.

  • More powerful when representing complex data.

  • Version control friendly

That's all for now. Please like ,share and comment 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.

Feel free to comment your views in the comment section.

Connect with me on Twitter