Have you ever encountered files with extensions .yaml or .yml while working on Vim editor or Linux terminal? Have you ever been stuck in an editor unable to exit? If so, then this is the right place to learn about it. The editor you were stuck in is called Vim, and the language is called YAML.
Most people learn YAML after being stuck in it for a while, trying to figure their way out. This was the case for me as well. I was stuck in it for almost 30 minutes, and it surprised me that I couldn't find a way out despite trying everything. But, let's dive deeper into what YAML is.
What is YAML?
YAML stands for YAML Ain't Markup Language. It was formerly known as Yet Another Markup Language. But what is a markup language? You might have heard of HTML (Hypertext Markup Language), which is a markup language. A markup language is a text-encoding system that consists of a set of symbols used to control the structure, formatting, or relationship between parts of a text document.
So, what's the difference between HTML and YAML?
HTML is used to document things, while YAML is not a markup language. It is a data serialization language. Data serialization is the process of converting data objects into a stream of bytes for easy representation.
YAML is used to exchange data, just like JSON (JavaScript Object Notation). However, YAML can store only data and not commands. It is used in configuration files, representing logs, and caches.
YAML is used in configuration files, representing logs, and caches.
YAML is human friendly it can work with most programming languages without any hassle.
YAML is used in Ansible and many configuration tools.
Easily Convertable to JSON/XML.
More Powerful when representing complex data.
YAML IS CASE-SENSITIVE: Yaml is case-sensitive.YAML does not allow the use of tabs. If you input apple and APPLE in YAML they'll be considered different.
Let's take a look at how YAML and JSON differ in their representation.
Representation In YAML vs JSON
- JSON representation.
YAML
It is clearly visible how easy it is to use YAML than other data serialization languages.
How does it work?
The basic building of YAML is the key-value pair. Making it resistant to delimiter collision.in the above picture of YAML name is a key and Server1 is a value .
TIP Always use a YAML linter, whether an online one or a plugin in your IDE.
Let’s make some hands-on experience with YAML…
- Commenting in YAML: Comments help developers know what the piece of code is doing In YAML Shortcut key for comments in YAML is CTRL +Q.In YAML only single-line comment is available NO MULTI-LINE COMMENTS. You have to use # for every line.
- A Single Line Comment is: # this is a single line comment. - Multi-Line Comments can be done using: # this is a multiple # line comment
2. Variables are defined using a colon and space:
integer: 46
string: "51"
float: 45.0
boolean: No
3. Multiple Documents in One file: If you want to have separate properties and values inside one YAML file. Simply, you have to start with 3 hyphens ( — -) and ends with 3 hyphens ( — -)
4. Formatting using YAML: If you want to see a single line you can do norma key-value but for lists, it is changed. Let's look at an example jar: I am a jam jar.
FOR This type of output we have to add the
jar: |-
I am not a normal
jar but a jar which can
glow in darkness.
To Wrap up
YAML is a data-serialization language that comes with multiple built-in advantages. It is human-readable and it is very much used in configuration building. Being Careful with indentation and whitespace one can easily learn them.