DevOps90DaysChallenge

Hi 👋there,

Now we are on the Day 15 task

Basics of Python(Libraries for DevOps)

Checkout the below link for Day 15 task:

https://github.com/LondheShubham153/90DaysOfDevOps/blob/master/2023/day15/tasks.md

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

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.

5 Ways to Convert Dictionary to JSON in Python | FavTutor

Function Used:

  • json.dumps()

  • json.dump()

Here we will be using json.dumps()

OUTPUT:

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

OUTPUT:

Here we first read the JSON file, on the left side of the image, kept in this folder and parse by using the loads() function then we print this file data by print function.

Read the YAML file using python, file services. yaml and read the contents to convert YAML to JSON.

PyYAML is a YAML parser and emitter for Python.

To install pyYAML module use command.

We need to use PyYAML module’s yaml.load() function. This function parses and converts a YAML object to a Python dictionary (dict object).

This process is known as Deserializing YAML into a Python.

  • First, we write the file data to the Yaml file using the dump() function & Then we fetch all data from Yaml by using the safe_load function.

  • File data is available in the YAML file as we see on the left side of the image.

  • Then we converted the Yaml file to a JSON file and formatted the JSON file can be seen on the right side image as a result.

Please, feel free to drop any questions in the comments below. I would be happy to answer them.

If you find this post helpful😊🙂, please follow and click the heart❤❤ button below to show your support.

_ Thank you for reading

_sandhya kumari