0 to 1 about YAML
All you need to know to get started and more
YAML is the language for you whether you're learning DevOps or a developer looking for API design or an ML enthusiast. It's mandatory to know the important concepts and this blog will right away help you with the same. So, let's get to Learning!
What is YAML?
YAML is a data serialisation language designed to be directly readable and writable by humans and is used by many programming languages. It was first proposed by Clark Evans in 2001, who designed it together with Ingy döt Net and Oren Ben-Kiki.
Initially, it was called "yet another markup language". Markup languages are languages of the web which are used in a way for formatting and arranging elements in a document. Some of the famous markup languages are HTML, XML, JSON. YAML is a superset of JSON, so JSON files are valid in YAML
Later YAML was repurposed as YAML Ain't Markup Language, a recursive acronym because it could work with data objects rather than just documents that usually markup languages work with.✔️
YAML was specifically created to work well for common use cases such as configuration files, log files and cross-language sharing files and data sharing.📄
What is Data Serialization?
- The process of converting data objects present in complex data structures into a byte stream for storage, transfer and distribution purposes on physical devices is called Data Serialization. Some of the data serialization languages are YAML, XML, JSON.
Image source: Google
- The above data serialization is done in XML. Similarly converting files to data objects is called deserialization.
Features of YAML ⚡
- Convertable to XML,JSON
- Follows the Indentation system just like Python Programming language.
- Can use parsers to read the data.
- You can only store data and not commands in YAML
- Mainly used in configuration files. ( defines the parameters, options, settings and preferences for computer application and OS in a file) eg. writing type of how the object should look like in Kubernetes.
Rules for Creating a YAML file
When you are creating a file in YAML, you should remember the following basic rules:
- YAML is case sensitive
- The files should have .yml or .yaml as the extension
- YAML does not allow the use of tabs while creating YAML files; spaces are allowed instead
Comments in YAML
YAML supports single-line comments Its structure is explained below with the help of an example: #this is a single-line comment.
One important thing to note is, YAML does not support multi-line comments. If you want to provide comments for multiple lines, you can do so as shown in the example below:
#this⚡
#is a multiple ⚡
#line comment⚡
YAML DATATYPES
YAML has three main types of data types:
-Scalar: Includes numeric datatype(int,bool, float,exponential) and string.
-Sequences: lists/arrays
-Mappings: dictionaries/hashes or key-value pair.
Below are some of the scalar data types representation. You don't need to specifically mention the type of the variable.
YAML OUTPUT To specify datatypes explicitly using tags use the following syntax:
var_name: !! datatype value
string1: !!str
zero : !!int 0
positiveNum: !!int 45
negative no : !!int -34
marks: !!float 56.89
date: !!timestamp 2022-01-10
YAML SEQUENCES AND MAPPING
- YAML is used to store key-value pairs and lists or sequences of elements. The KEY represents the name and the value represents data separated by : (colon + mandatory space). The key-value pairs are called maps.
YAML SEQUENCES: Yaml sequences could be lists or arrays. Sequences can also be nested.
- A list is an ordered collection of data. It is represented with hyphen + space. Each list item is in a new line. YAML is case-sensitive hence Monday and monday are two different list items.
- Array representation in block style. A document in block style uses spaces to structure the document. It is also called "Mapping of Sequences " as months is a map value and hyphen followed by space are the list items.
Pictorial representation :
- Sequence of Mappings: We can also create a sequence that contains mappings values as shown below.
JSON equivalent:
{
"months":
[
{ "jan" : "month1" , "feb" : "month2"}
]
}
There is also mapping of mappings and sequence of sequences concept. Feel free to try it on your own.
- YAML has an alternate syntax called flow style, which allows sequences and mappings to be written inline without having to rely on indentation. It uses [ ] brackets for mapping and { } brackets for sequences.
Note that: YAML is a collection of one or more documents. Separate these documents with ---(three hyphens) and use ...(three dots ) for the end of the file.
Ps: Do check out other advanced data types and concepts. The link to the recommended video has been shared below.
YAML Anchors and Alias:
Anchor and Aliases allow you to extend the properties and promote reducibility. Anchors declared using & sign define certain properties, whereas to refer to those properties we use Aliases using * (asterisk)
You can override with the characters '<<:' to add more values or override existing ones. In the code shown below, the radius value is overridden from 13 to 14mm.
child: &base
shape: circle
radius: 13mm
parent1:
type: semicircle
<<: *base
radius: 14mm
Some of the great resources and references that I've used to learn YAML
- Kunal Kushwaha's complete YAML video- This video gets all the credits for my YAML learnings. Highly recommended.
- Wikipedia
- YAML to JSON converter tool
Thanks for coming so far. This is my first blog. Planning to write a lot more in near future. If you find the blog useful in any way don't forget to like, share and comment. You know the drill ;) Also, feel free to give your feedback about it.
Thank you for your patient reading <3