YAMLmao: Understanding the 'Ain't Markup Language'

YAMLmao: Understanding the 'Ain't Markup Language'

🚀Introduction

You might have heard of JSON or XML, but have you heard of YAML? YAML (Yet Another Markup Language OR YAML ain't markup language) is a data serialization format that is becoming increasingly popular for its simplicity and ease of use.

🚗What's data serialization though?

Data serialization is the process that converts structured data into a format that can be easily stored, transmitted, or processed between different systems.

Deserialization is the reverse process - it takes the serialized data and converts it back into its original format.

Here's an example:

  1. System A has a database of customer information, including name, address, and phone number.

  2. System B is a web application that needs to display this customer information to the user.

To exchange the customer data between these two systems, System A can serialize the data into a standardized format (serialization), such as JSON or YAML, and transmit it to System B over a network.

Customer data in JSON format:

[ { "name": "Elon Musk", "address": "street 123", "phone": "1101101101" }, { "name": "lorem ipsum", "address": "street 456", "phone": "2202022022" }]

System B can then receive this data and parse it, restoring the original customer information data structure (deserialization). The web application can then use this data to display customer information to the user.

📈Why use YAML over JSON or XML?

YAML is preferred over JSON or XML because:

  • it is more readable and concise.

  • better supports complex data structures.

  • supports dynamic typing, meaning that values don't need to be declared with a specific data type.

  • represent the same data as JSON or XML using fewer characters (as seen in the above image).

However, JSON and XML have their advantages, they are better suited for handling large amounts of data.

Therefore select according to the needs of your project.

💻Let's code YAML!

  1. Simple key-value pair:

     name: YaMl
     age: 32
    
  2. List of values:

     fruits:
       - Apple
       - Banana
       - Orange
     - Mango #THIS IS WRONG!
    

In the case above, indentation is a must! Though you can also write a list as:

fruits: [Apple, Banana, Orange]

Both of these are valid!

  1. Nested Elements:

     person:
       name: yAmL
       age: 40
       address:
         street: 123 street
         city: San Francisco
         state: CA
    
  2. Multi-Line Strings:

     wrong: #wrong method
       This is the wrong 
       method for multi-line 
    
     correct: | #add a bar
       This is the correct 
       method for multi-line
    
  3. Other:

price: 12.99
is_available: true
#true can also be written as: yes, y, Y, True, TRUE
#false can also be written as: no, n, N, False, FALSE

➕DataTypes in YAML

Notice how we did not mention datatypes in any of the codes above?

As specified above, YAML supports dynamic typing, but if required you can still specify the datatypes using type tags!

Example:

Here, !!int, !!float etc are type tags.

name: yAMl
age: !!int 53 #for integer type 
height: !!float 6.14 #for float type 
address: !!str "123 street, USA" #for string type 
is_employee: !!bool true #for boolean type

Practice YAML and check its validity, transform it etc.

YAML introduction

YAML In-depth

Blog on YAML

🟡Conclusion

To conclude, YAML is a simple and readable way to store and exchange data. It uses a clean syntax and allows you to specify the type of data being stored. This makes it a convenient choice for organizing information in small to large projects.