# Day 15 Task: Python Libraries for DevOps🚀

Today, let's dive into a crucial aspect of being a DevOps Engineer - reading JSON and YAML in Python. 🐍📚

### 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.

```python
import json

# Create a dictionary
python_dict = {
    'name': 'john',
    'age': 30,
    'city': 'calcutta'
}

# Write the dictionary to a JSON file
json_string = json.dumps(python_dict)

print(json_string)
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1700678968750/d08b57f5-ac1c-4be2-9cd4-dc90244e9787.png align="center")

### 💼Tasks : 2

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

```python
import json

with open ("services.json", "r") as json_file:

	data = json.loads(json_file.read())
	print(("type: ", type(data)))
	print("aws:", data['services']["aws"]["name"])
	print("azure:", data['services']["azure"]["name"])
	print("gcp:", data['services']["gcp"]["name"])
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1700681430133/84c45c51-7f5f-4d14-906c-9677bc7b89ad.png align="center")

### 💼Tasks : 3

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

Before writeing the code we need to install **PyYAML** module in order to use yaml.load() function

`pip install pyyaml`

```python
import yaml
from yaml.loader import SafeLoader

with open ("services.yaml") as yaml_file:
	data = yaml.load(yaml_file, Loader=SafeLoader)
	print(data)
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1700682741845/724071ff-8e6b-47eb-af38-9521e1b456ef.png align="center")

**Convert yaml to json :**

```python
import yaml
import json

with open ("services.json", "r") as json_file:
 	data = yaml.safe_load(json_file)
print(data)
	
with open ("new.json", "w") as json_file1:
	json.dump(data,json_file1)

output=json.dumps(json.load(open('new.json')), indent=2)
print("json_file :\n",output)
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1700684186829/40a8ac85-8c3f-46c2-9dfb-9b471814f2d1.png align="center")

### **📍** Conclusion :

In this blog, we explored working with JSON and YAML in Python using the **json** and **PyYAML** libraries. We covered tasks such as creating a dictionary and writing it to a JSON file, reading a JSON file and extracting specific information, and converting YAML data to JSON format. These techniques can be useful when dealing with configuration files or processing data in various applications.

Thank you for reading!

Contact me on : [Linkedin](https://www.linkedin.com/in/sutish-pradhan/) 🤝

Check out my [**GitHub**](https://github.com/sutish) for more resources 📚
