YAML For Data Representation?
YAML is a better option than JSON when comes to representation of Data.
About a year ago I have come across a very πͺ powerful but with minimalist way of representation of data called YAML.
Before YAML I was using JSON like any other developer. I have mostly used JSON for representing data in config files. But due to its design of using {}
for defining separating data makes the file bigger and I have to scroll it many times up and down to read and understand the config file.
As my favourite programming language is Python, I prefer separation of data using indentation instead of brackets, that way is looks clean to me.
One day my senior told me about the YAML files he is being using it and finds it better than JSON for data representation.
YAML is Simple yet Powerful tool
So, I jumped my ship π’ from JSON to YAML.
π Let's explore what is YAML and why everyone prefers YAML over JSON when it comes to data representation.
β¨ WHAT IS YAML?
YAML is a data serialization language. It stands for "YAML Ainβt Markup Language" but when it was first came out in 2001 , it stands for "Yet Another Markup Language". They have to later change it to make it clear that language is intended for data and not documents.
Its similar to XML and JSON but with a minimalist syntax, which makes it more human-readable.
Many Devops Engineer and Developers prefer it for writing configuration files.
Features from other languages:
- Scalars, lists, and arrays come from Perl.
- The three-dash separator comes from MIME.
- Whitespace wrapping comes from HTML.
- Escape sequences come from C.
Things to keep in mind when using YAML
- YAML files should with .yaml
- YAML is case sensitive.
- YAML does not allow the use of tabs(only spaces).
β¨ Lets see what YAML is offering
Some best features of YAML
Multi-document support
In YAML, you can define multiple documents within one document with the help of ---
.
This makes it very easy to define multiple documents and also keeps the representation clean.
---
Employee_ID: emp_110
Department: Accounts
---
Employee_ID: emp_115
Department: Sales
Commenting
YAML has been built-in option for commenting. It uses hash symbol #
similar to Python.
# This is a single line comment
Employee_ID: emp_110
Department: Accounts
# This is a
# Multi-line comment
Implicit and Explicit typing
YAML supports dynamic typing similar to Python but it also give user option to explicitly define the type of the data. To define the type of data just add !![typeName]
before the value.
# This is string type
Employee_ID: !!str emp_110
# This is string type
Department: !!str Accounts
# This is boolean type
Work_in_HQ: !!bool true
# This is int type
Work_experience: !!int 4
β¨ Let's see Data Types available in YAML
scalars (strings / numbers)
YAML supports strings and numbers to be stored as data. The basic format is key
followed by :
followed by space and then value
. Value can be string or integers or floats.
# String Datatype
Employee_name: Abhishek
# Integer Datatype
Work_experience: 4
# Float Datatype
Weight_in_kg: 72.5
Mappings (hashes / dictionaries)
Dictionaries are a collection of key-value pairs. Its useful define mappings. We can easily define nested statements using indentation.
Let's see an example to understand.
# Mapping for employee data
Employee_ID: emp_110
Department: Accounts
Personal_details:
City: Delhi
Country: India
Phone_number: 123456
fitness_data:
weight_in_kg: 72.5
height_in_cm: 171
As you can see we have define two nested mappings personal_details
and fitness_data
sequences (arrays / lists)
We can also represent data a sequence similar to lists in python. we just need to use hyphen symbol -
to mark next element in list.
Let's understand by example
# List of Employee IDs
Employees:
- EMP101
- EMP102
- EMP103
Use can also use []
to define a list or array.
# List of Employee IDs
Employees: [EMP101, EMP102, EMP103]
β¨ Concluding
You can also check out the official documentation of YAML. I have been using it since a year now and finds it more helpful than JSON for writing my configuration files.
π Do give it a try and if you found this article any helpful, please let me know by showing your support. π