0 to 1 about YAML

All you need to know to get started and more

0 to 1 about YAML

Photo by NeONBRAND on Unsplash

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.

data_Serialization.png

Image source: Google

  • The above data serialization is done in XML. Similarly converting files to data objects is called deserialization.

Features of YAML

  1. Convertable to XML,JSON
  2. Follows the Indentation system just like Python Programming language.
  3. Can use parsers to read the data.
  4. You can only store data and not commands in YAML
  5. 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:

  1. YAML is case sensitive
  2. The files should have .yml or .yaml as the extension
  3. 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_input.PNG YAML OUTPUT yaml_output.PNG 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. Screenshot_key_value.png

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.

Screenshot_list.png

  • 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.

Screenshot_array.png

Pictorial representation : array picPNG.PNG

  • Sequence of Mappings: We can also create a sequence that contains mappings values as shown below.

Screenshot_mapping to sq.png 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.

Screenshot_{ } eof.png

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

  1. Kunal Kushwaha's complete YAML video- This video gets all the credits for my YAML learnings. Highly recommended.
  2. Wikipedia
  3. 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

Did you find this article valuable?

Support RuhikaBulani by becoming a sponsor. Any amount is appreciated!