Introduction to YAML...

Introduction to YAML...

Before starting, I want you all to know that this whole article is based on my learning from - Kunal Kushwaha's YAML Course and my experience with YAML so far.

YAML

  • YAML stands for "YAML Ain't Markup Language".

  • It is neither a programming language nor a markup language like HTML and XML.

  • It is a text-based language used to store data in a human-readable format and so it is easy to read and understand by anyone.

  • It is often used for writing configuration files for Kubernetes.

  • Extension: .yml

  • In YAML, we can only store data and not commands.

How a YAML file looks?

  • Below you can see how a yaml file looks.
--- 
fruits: "yes"
intro: "hi! I am Rituj"
yaml: "YAML Ain't Markup Language"
year: 2022
...

Comments in YAML

  • Comments in YAML can be declared using '#'.
 #This is a comment. 
--- 
fruits: "yes"
intro: "hi! I am Rituj"
yaml: "YAML Ain't Markup Language"
year: 2022

Creating a YAML File

  • All YAML file starts with three dashes. These dashes indicate the start of a new YAML document.

  • We use ' --- ' to distinguish the list, block, and other components from each other.

  • We end our YAML file with ' ... '. After this, nothing is written in that specific YAML file.

Components in YAML

1. Key: Value Pair

"Apple": "A red fruit!"
1: "Roll Number"

2. Lists

--- # To start the list/ separate/ distinguish the list
- YAML
- HTML
- XML
- JSON

3. Block Style

  • represents a series of nodes.

  • each item is denoted by a leading " - ".

---
fruits: 
 # Indentation is important (like Python)
 - Apple 
 - Mango
 - Banana
 - Strawberry
 - Grapes

YAML VALIDATION

  • When we write a code be that in any programming language. We run it to check the errors to make sure the code we wrote is working fine so that it doesn't cause any problem at runtime.

  • The same way, we need to validate our YAML file to check if it is correct, if not we can modify it and validate it over and over again till it's perfect.

  • You can validate your YAML file on YAMLlint.

Data Types in YAML

1. Strings

  • YAML strings are Unicode so we don't always have to specify them in quotes.
---
me: hey! I am Rituj.
single_quote: 'hey! this is a single quote'
double_quote: "we are in double quotes"
  • but if we want escape sequences handled, we need to use double-quotes.
---
double_quote: "we are in double-quotes \n we are in another line"
  • after processing, the above code will look like this
--- 
double_quote: |-
    we are in double-quotes 
     we are in another line
  • String values can span more than one line. With the fold (greater than) character, you can specify a string in a block.
one_liner: >
  this is not a normal string it
  spans to more than
  one line.
  • On validating the above script, we get:
--- 
one_liner: "this is not a normal string it spans to more than one line.\n"

Specifying data type in YAML

  • If you want to specify a data type, say - an integer.
--- 
number: !!int 100
binaryNumber: !!int 011001

2. Numeric Types

  • YAML recognizes numeric types. YAML supports several numeric types such as decimal, hexadecimal, or octal.
--- 
# representing 659 in binary, hexadecimal and octal
binary: 0b1010010011
hex: 0x293
octal: 01223
  • After validation, we get:
--- 
binary: 659
hex: 659
octal: 659
  • We can even represent infinity and NAN values.
---
inf_num: .inf
nan_num: .NAN
  • On validating the above script, we get:
--- 
inf_num: Infinity
nan_num: NaN

3. Boolean

  • YAML indicates boolean values with the keywords True, On, and Yes for true. False is indicated with False, Off, or No.
---
value_1: True
value_2: False
light_on: On
light_off: Off
  • On validating our script, we get:
--- 
light_off: false
light_on: true
value_1: true
value_2: false

4. Sequence Data Type

--- 
student: 
- marks
- names
- roll_no

# we can also write it in one line

student: [name, roll_no, marks]
  • Sparse Sequence It could happen that some keys of the sequence are empty.
---
sparse seq:
- name
- 
- marks
- Null
  • Nested Sequences
---
- 
- mango
- banana
- strawberry
-
- name
- roll_no
- marks
  • On validating the nested sequence, we get:
--- 
- ~
- mango
- banana
- strawberry
- ~
- name
- roll_no
- marks

5. Maps

  • key: value pairs are called maps.

  • start with "!!map". See the example below:

---
!!map
name: Rituj
age: 78
job: student

6. Sets

  • A set is an unordered collection of nodes such that no two nodes are equal.

  • it allows us to have unique values.

  • represented with " !!set".

---
# Explicitly typed set.
richest people: !!set
  ? Jeff Bezos
  ? Elon Musk
  ? Warren Buffett
  ? Mark Zuckerberg

7. Dictionary

  • Nearly every YAML file starts with a list. Each item in the list is a list of key/value pairs, commonly called a “hash” or a “dictionary”.

  • A dictionary is represented in a simple key: value form.

---  
# An employee record
martin:
  name: Dudley Lopez
  job: Consultant
  skill: Elite

-- This was the summary of my learning on YAML--