Open In App

What Type of Language is YAML?

Last Updated : 29 Jan, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

As we know the world of technology is filled with various modern and old languages which keep changing from time to time, each one of these languages serves a specific purpose and thus each language is used for performing certain tasks only. But when it comes to the programming language called YAML people sometimes get confused because they have trouble understanding what type of programming language YAML is, so in this article, we will understand what type of language the YAML programming language is.

Introduction

The YAML is an open-source language used for writing configuration files on your system, what makes the YAML language different from other computer languages is that it is human-readable, unlike other programming languages which makes it easier to work with.

yaml-banner

What Type of Language is YAML?

What is YAML?

The full form of the YAML language can be different depending on whom you are asking it, mostly there are two full forms of this language and both of them are considered to be correct, the first one is yet another markup language while the other full form is YAML ain’t markup language. if you read the second full form of YAML, you will understand that the YAML is not a markup language which means that this language is used for data and not documents.

YAML syntax:

The syntax of the YAML programming language is very easy to understand and time efficient because it focuses on generating human-readable codes, here are some of the syntax rules for YAML:

Indentation: Just like python, the YAML language also heavily works on the basis of indentation in order to represent the nesting of the data as well as the hierarchy of it.

Note: In YAML, spaces are used for indentation rather than tabs.

Lists: YAML also allows us to create lists if we want, in YAML the lists are based on arrays which are a sequence of items. In YAML, each item which begins with a dash (‘-‘) along with a space is known as a list.

For example:

“`yaml
hobbies:
Catering
Cooking
Coding
“`


Key-Value pair: This contains data which is stored in key value pairs and are separated by a colon.

Example:

"name:Kishan Kaushik"


File Extension: YAML files are saved with a file format that ends with either “.yaml” or “.yml

As you can see in the image below, the visual studio code provides the option to save the file as .yaml extension.

yaml01-(1)

Visual studio code – save file as .YAML.

YAML Scalar syntax:

YAML scalar means that a simple value for any key, as we discussed later that the scalar can be float, Boolean, integer etc..The scalars are known to be the basic blocks that are required for writing the YAML language and they can be complex as well as simple depending on the requirements. The scalars can be written in various possible ways, let’s look at some of them as example:

1. Plain Style:

In the plain style of scalars in YAML, the scalars can be some simple strings which does not include any special characters or quotes, for example you can see:

key: value

2. Single-Quoted Style:

The scalars can also be closed in the single quotes this style is mostly used whenever you want to specify a string just the way it is without any special characters, for example:

key: 'value'

3. Double-Quoted Style:

Just like the single quotation marks we can also use the double quotations for the strings the only difference here is that the double-quoted string allows escape sequence to be interpreted, for example:

key: "value with\nnewline"

4. Flow Style:

Scalars can also be written in a more compact flow of style. this is suited for simple values and short values, for example:

key: { foo: bar, baz: 42 }

XML VS JSON VS YAML – What’s The Difference?

All the above three languages are very popular data serialization languages used in the today’s industry for computer science and IT development, but which among them is better? and what is exactly the difference between each of them? for answering these questions we will have to take a look at the difference between these languages.

So let’s look at the table below which differentiates the XML, JSON and YAML language:

XML

JSON

YAML

XML stands for eXtensible Markup Language.

JSON stands for JavaScript Object Notation.

YAML stands for YAML Ain’t Markup Language.

XML is a markup language.

JSON is a data format.

YAML is a data format.

JSON has no concept of comments.

XML allows you to add comments but only within a document.

YAML is designed to make more readable content and thus allows comments.

The XML language supports some complex data types such as charts or images.

Unlike XML, JSON supports only strings, numbers, arrays, boolean, and objects.

YAML can support data types such as date as well as time stamps, sequences, nested, recursive values or primitive data types.

It is somewhat difficult to read or interpret the data if you are using XML.

It is somewhat easier to easy to interpret data in JSON format.

It is much more easier to read the data in YAML than in JSON format.

XML is used for data interchange.

JSON is used better as a serialization format.

YAML is used and suited best only for configuration.

XML is considered to be bulky and somewhat slow in parsing.

The JSON files are smaller compared to the XML file and thus quickly parsed by the JavaScript engine.

YAML is considered as a superset of JSON so it also can deliver a faster data transmission.

What Type of Language is YAML?

Now let’s come back to our main question, what type of language is YAML? the YAML inherits the “ML” from the HTML and SGML languages which are known to be markup languages but the YAML itself is not a markup language because it is a unicode based data serialization language.

The main reason for YAML not being a markup language is because the data model of the YAML language only contains tree structure and when we look underneath we will not see any linear text that the tree applies to, because there is nothing to mark up in it.

Features and basic rules of YAML:

There are some basic rules that you must follow whenever you are writing the YAML, those rules are the following:

  • The file must have .yaml as the format or extension while being saved.
  • The YAML language is case sensitive.
  • The YAML language does not provide you with the ability to use tabs whenever you are creating YAML files but you can however you the spaces instead of tabs.

Whenever you are writing using the YAML language then you must follow these rules mentioned above so that you don’t encounter any errors.

Features of YAML:

As we have already discussed that the YAML provides us with human readable code which is a feature in itself but let’s look at a few features that the YAML language provides which makes it better suited language that it’s competitors:

  1. It provides us the ability for cross-language portability of data.
  2. It provides us with a one-pass processing.
  3. The YAML language can provide a data model that is very consistent.
  4. YAML language provides us the ease for use as well as implementation of the code.

How to create a YAML file?

It is very easy and simple to create a YAML file, there are mainly three ways to create a YAML file if you want, let’s see what those ways are and also understand each method one by one.

Method 1: Use Any Online Tool for YAML or Text Editor:

Step 1: Use any online tool that helps you to convert the code into indented YAML data,

Step 2: for example we have the following data that we will use for conversion:

apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
labels:
app: nginx
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.14.2
ports:
- containerPort: 80

Step 3: Then simply go to any online converter website for YAML, for example I have this website that can do so:

https://jsonformatter.org/yaml-parser

Step 4: This will give you the YAML code and then you can download the YAML file as well by using the download button.

yaml-parser

YAML Parser.

Method 2: Create a file using the YAML URL:

As you know that some developers also work with data and file that are stored on the cloud servers and almost 90% of the data is hosted mostly on the Azure, Google Cloud, DigitalOcean etc. so developers can simply use the remote URL to save and edit the YAML files that are stored in the servers.

Here is a sample of the YAML URL. Now you can simply click on these YAML sample to view it.

https://github.com/kishan-kaushik/git-project/blob/master/file.yaml

Method 3. Create the YAML file using YAML Template:

YAML is used for configuration files which means that we need to avoid most of the mistakes for this you always need to take precaution and use the right template, for example:

apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
labels:
app: nginx
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.14.2
ports:
- containerPort: 80

Multi-document support in YAML:

YAML itself doesn’t inherently support multi-document structures like JSON arrays or objects. YAML is primarily designed to represent a single data structure. However, there is a way to handle multiple documents within a single YAML file using the — document separator.

# Document 1
key1: value1
key2: value2

---
# Document 2
key3: value3
key4: value4

As you can see that we have used “—” serves as a document separator.

Indentation in YAML:

YAML uses the indentation in order to indicate both the structure as well as the hierarchy of the data. the recommended indentation for the YAML language files is simply two spaces per level but apart from that YAML can also follow any of the indentation which the individual file uses.

New-Project

indentation in YAML.

Tabs in YAML:

The YAML language does not allow the using of the TAB button because of indentation issues. You can however use any amount of spaces and combination of space instead of using the tabs in the YAML language.

Whitespace in YAML:

YAML is a language that is known to be whitespace-sensitive which means that while indentation defines the structure. however it does not accept any tabs as we discovered earlier for the indentation. so that means that the empty lines are ignored and comments are written using octothorpe #.

Why use YAML instead of JSON?

Many people also ask the question, that why should we use the YAML instead of using JSON, one of the most easiest and simplest way to answer this is because the syntax of YAML is very easy to read, understand and write as compared to JSON, which makes it very easy to write and understand not only for the developers but also the other non-developer members of the team.

Explicit data types in YAML:

In the YAML language, custom data types are allowed but the YAML language is known to natively encode the scalars, if you are wondering what scalars are then simply know that strings, float values, strings are known to be scalars.

Conclusion

In conclusion, we learnt that the YAML language is a data serialization language and not a markup language, apart from that we also looked at why we should use the YAML language instead of using JSON language as well. you can use the YAML language if you want to write simple-to-understand codes for your configuration files.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads