In this Article (Click to skip to that topic):
- YAML: What is it?
- Why do you need YAML?
- How to use PyYAML to use YAML Files?
- Advantages of using YAML and PyYAML function
- Convert JSON to YAML in one click
What is YAML?
What if you had a very easy, simple, readable way to feed data dynamically to your program, regardless of the programming language using which it is written?? All of this by just writing a file with no programming knowledge?
YAML: YAML Ain't Markup Language. YAML is a human-friendly way to send data such as configuration data to your program. Just by editing the YAML file, the program will be able to read the values from the YAML File and use it.
Ever used JSON files? YAML can be considered a more readable version of JSON files.
Why do you need a YAML File?
Consider this situation. You have a program which installs a set of software onto your computer. This program can be used on many computers. Let us assume that, these are the computer-specific data that is necessary to run the program on the computer:
- This Computer's username
- This Computer's password
- The path in which the software must be installed in
- The specific set of software that has to be installed in this particular computer
If you were using the usual programming way of doing this, you would either
- Take in the inputs through some command-line arguments ( which is difficult for non-programmers to use )
- Change the code every time you run on different machines ( that isn't a good way at all )
So what if I told you, you can have a text file, which is human readable, independent of the programming language that you are using, win which you can just fill in the above 4 data according to the computer, and the program will take care of the rest?
That is exactly what YAML helps you with.
Creating your first YAML File
Step 1: Create a file by any name, say config.yaml
. Make sure to use the extension .yaml
to the file.
Step 2: A YAML file should start with the ---
like
---
# YAML files start with ---
# comments begin with a #
# and they can be written anywhere
Step 3: You can have the keys in the YAML file, which may or may not hold any values in them. Below we have keys such as username, password, path, VLC Media Player etc., which we can use later in the program to grab their respective values.
---
username: Shiv
password: shiv@456
Note: YAML is case sensitive.
Step 4: You can have a list of items under a key, even nest them further
---
username: Shiv
password: shiv@456
path:
- VLC Media Player: /etc/vlc
- Visual Studio Code: /etc/vscode
- Google Chrome: /etc/chrome
softwares:
- VLC Media Player
- Visual Studio Code
- Google Chrome
- Git Bash
- Video Convertor
- Node Js
So using the YAML syntax, we have easily listed the data we will need, to feed to our application.
Step 5: Now just save this file as config.yaml
How to use PyYAML to use YAML Files?
Now we have the config.yaml
, all that we have to do is use it in our program. Say you are writing a Python program that uses this config file and installs the software. We will have to use a python package called PyYaml.
Step 1: Install pip ( Install Python from their website first if you don't have it )
- Download get-pip.py to a folder on your computer.
- Open a command prompt and navigate to the folder containing get-pip.py.
- Run the following command:
python3 get-pip.py
- Pip is now installed!
Step 2: Install PyYaml package now
pip3 install pyyaml
Step 3: Create a new Python File and open it - say test.py
and add the following code to it, where we import using PyYAML
import yaml # import pyyaml package
# open the yaml file and load it into data
with open('config.yaml') as f:
data = yaml.load(f, Loader=yaml.FullLoader)
print(data)
Output:
{
'username': 'Shiv',
'path': [
{'VLC Media Player': '/etc/vlc'},
{'Visual Studio Code': '/etc/vscode'},
{'Google Chrome': '/etc/chrome'}
],
'password': 'shiv@456',
'softwares': [
'VLC Media Player',
'Visual Studio Code',
'Google Chrome',
'Git Bash',
'Video Convertor',
'Node Js'
]
}
Step 4: Let us try to pick what we need from the config file
from __future__ import print_function
import yaml
with open('config.yaml') as f:
data = yaml.load(f, Loader=yaml.FullLoader)
username = data['username']
password = data['password']
# printing the username
print("The username is ", username)
print("The password is ", password)
Output:
The username is Shiv
The password is shiv@456
Here we have grabbed the username and password from the data. Next, let us grab the software list from the config file.
Step 5: Grabbing the software list
from __future__ import print_function
import yaml
with open('config.yaml') as f:
data = yaml.load(f, Loader=yaml.FullLoader)
username = data['username']
password = data['password']
path_list = data['path']
software_list = data['softwares']
# printing the username
print("The username is ", username)
print("The password is ", password)
# the software list
for software in software_list:
print("Installing ", software, "...")
Output:
The username is Shiv
The password is shiv@456
Installing VLC Media Player ...
Installing Visual Studio Code ...
Installing Google Chrome ...
Installing Git Bash ...
Installing Video Convertor ...
Installing Node Js ...
Since the softwares are stored in a list, we can easily run a for loop and grab each software in the software variable and print it.
Step 6: Printing the paths where each software must be installed
Now, this isn't complex, so just analyze it. We are first looping over the list of items in the path_list
, and then for each of those list item, say {'VLC Media Player': '/etc/vlc'}
, we will now loop over softwares in the software_list
to find if the software matches the key of this list item, so if 'VLC Media Player' from the path_list
matches the 'VLC Media Player' from the software_list
, then it prints the path of it. ( Read and analyze it again if you didn't get it, you will get it for sure. )
from __future__ import print_function
import yaml
with open('config.yaml') as f:
data = yaml.load(f, Loader=yaml.FullLoader)
username = data['username']
password = data['password']
path_list = data['path']
software_list = data['softwares']
# printing the username
print("The username is ", username)
print("The password is ", password)
# the paths are
for path_dictionary in path_list:
for software in software_list:
if software in path_dictionary:
print("Install ",software," in ",path_dictionary[software])
# the software list
for software in software_list:
print("Installing ", software, "...")
Output: ( drum rolls please....)
The username is Shiv
The password is shiv@456
Install VLC Media Player in /etc/vlc
Install Visual Studio Code in /etc/vscode
Install Google Chrome in /etc/chrome
Installing VLC Media Player ...
Installing Visual Studio Code ...
Installing Google Chrome ...
Installing Git Bash ...
Installing Video Convertor ...
Installing Node Js ...
Advantages of using YAML and PyYAML? Why Should you use them?
1. Readable and easy to write
The YAML file is easy to read as it doesn't contain any extra symbols other than the essential colons and spaces
2. Write once, use everywhere
The same config file can be used for any programming language without modifications.
3. Change values easily
Just by changing the values in one YAML file, it will get reflected in the program, thus need not go through the pain of changing values everywhere in the program, or search for the variable in the program that holds this value.
Have a JSON file and wanna convert to YAML?
Check out this website where you can convert JSON into YAML : JSON to YAML - online convertor
Article originally written by Tharun Shiv, on https://tharunshiv.com
Written by,
Top comments (13)
Which do you think is more used in the Industry? YAML vs JSON
As far as I've noticed, JSON is used in places where the data transfer takes place , and the data is parsed, and the user doesn't manually deal with it.
YAML files are used when user deals with providing inputs or configuration.
See the main advantage of YAML is readability. So the user is less prone to errors like missing a comma or missing a curly bracket. π
JSON is used for anything API related. Any payload of data transferred between servers, clients, or any other API is very likely to be JSON, unless its XML
YAML is generally used for configuration purposes, although JSON is quite popular for that purpose too.
Yes, that's on point. Thanks π
JSON. But for DevOps you will need yaml
I absolutely hate YAML. I thought it was only going to be like that only when I started using it, that it would get better with time, but it really hasn't.
My personal opinion:
I agree with you in the points you dislike. They are definitely an issue.
However, it is more readable than JSON for non-developers. And sometimes that is more important.
Yes Nico. You just give them a template and they will easily fill it in, in case of YAML.
Thanks for sharing your perspective Sebastijan. π
Me too.
I find it's indentation poor. Curly braces are works for me. I can easily follow the nesting π―
Excellent explanation of how YAML is used! Your example makes everything very clear.
Thanks
Thanks a lot Bruce! This motivates me to write more... π
I don't think it's readable than JSON.
I find it hard to follow. Due to lack of curly braces. I dislike indentation poor when nesting.