Day 15 Python Libraries for DevOps

Day 15 Python Libraries for DevOps

Day 15 of #90daysofdevops

Hey Techies! Welcome to this blog

In this blog, we are going to start Python Libraries for DevOps

Reading JSON and YAML in Python

  • As a DevOps Engineer, you should be able to parse files, be it Txt, json, yaml, etc.

  • You should know what libraries one should use in Python for DevOps.

  • Python has numerous libraries like os, sys, json, yaml etc that a DevOps Engineer uses in day-to-day tasks.

Tasks:

  1. Create a Dictionary in Python and write it to a json File.

JSON stands for JavaScript Object Notation. It means that a script (executable) file which is made of text in a programming language, is used to store and transfer the data.

Function Used:

  • json.dumps()

  • json.dump()

Here we will be using json.dumps()

import json

x = {
    "Name" : "Kritika",
    "Age": 23,
    "File" : "MyFile"
}

# convert into JSON
y = json.dumps(x)

print(y)

This code snippet creates a dictionary called x, containing information about a person. The json.dump() function is then used to convert into JSON.

  1. Read a json file services.json kept in this folder and print the service names of every cloud service provider.
output

aws : ec2
azure : VM
gcp : compute engine

There are two essential functions of the JSON module -

  1. json.dump() - To write the JSON file.

  2. json.load() - To read the JSON file.

Here we will use json.load function to read the json file.

service.json file

import json

with open("service.json") as jsonfile:
  jsonObject = json.load(jsonfile)
  jsonfile.close()
print(jsonObject)
print(output)
print("aws : ", jsonObject["services"]["aws"]["name"])
print("aws : ", jsonObject["services"]["azure"]["name"])
print("aws : ", jsonObject["services"]["gcp"]["name"])

No alt text provided for this image

Steps:

  1. Import the json module.

  2. Open json file using the open() function

  3. Read jsonfile using json.load() function

  4. Close the jsonfile using the close() function

  5. Print the name of the services in the output

  6. Read the YAML file using Python, file services.yaml, and read the contents to convert yaml to json.

To convert the yaml file to json, we will first convert it to a Python dictionary and then to a json string.

Steps:

  1. We will open the YAML file with an open() function in yaml module.

  2. We will use yaml.load() function in the yaml module to take the yaml file as input and convert it to a Python dictionary.No alt text provided for this image We mention the type of loader in the Loader argument.

  3. Next, we will use json.dumps() function to convert the Python dictionary to json string.

  4. At last, we will print the json string to get the output.

import json  
import yaml 
from yaml import SafeLoader  
with open("service.yaml") as yaml_file:  
  py_dict = yaml.load(yaml_file, Loader=SafeLoader )      
  json_object = json.dumps(py_dict, indent=4)

print("Yaml is converted to json", json_object)

No alt text provided for this image

Thank you so much for taking the time to read till the end! Hope you found this blog informative and helpful.

Feel free to explore more of my content, and don't hesitate to reach out if need any assistance from me or in case of you have any questions.

Happy Learning!

~kritika :)

Connect with me: linkedin.com/in/kritikashaw