# Python Libraries for DevOps

#90DaysOfDevOps

# 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 all 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.**
    
    The JSON module can be used to generate a dictionary in Python and write it to a JSON file:
    

**json.dumps()** : It is used to convert a dictionary to JSON string.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1680065024051/f7b10cff-8bbb-4845-a3d5-d9b44bf4714b.png align="center")

* Read a JSON file `services.json` kept in this folder and print the service names of every cloud service provider.
    
    **json.load()** accepts a file object, parses the JSON data, fills a Python dictionary with the data, and returns it to you.
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1680065705650/75f73cef-c113-4711-acf4-5a98cdcabc30.png align="center")

* **Read YAML file using python, file** `services.yaml` **and read the contents to convert yaml to json.**
    
    First, use the import statement to import the yaml module.
    
    Using the open method, read the file.
    
    Method of the safeloader reads the contents of the file and converts it to a dictionary Python object
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1680066433822/00b38fe6-0e4c-437a-bd42-de7cc4156b58.png align="center")

**Convert yaml to json:**

**json.dumps()** function will convert a subset of Python objects into a json string.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1680066956606/6e093b57-a361-4166-9a0f-c2226933ec2f.png align="center")

**Thank you for taking the time to read this article. I hope you enjoy it. Please follow me for more devops-related blogs.**

**~Prabhakar Yadav**
