Python DevOps Magic: Easy JSON & YAML Handling โจ๐ Day - 15
Hello DevOps enthusiasts! ๐ Today, let's explore the simplicity that Python brings to our daily DevOps tasks, specifically when it comes to working with JSON and YAML files.
Crafting Cloud Chronicles with Python Magic
Creating a JSON file for your cloud services is as easy as brewing your favorite potion:
import json
# Cloud services potion
cloud_services = {
"aws": "ec2",
"azure": "VM",
"gcp": "compute engine"
}
# Save it to 'services.json'
with open("services.json", "w") as json_file:
json.dump(cloud_services, json_file)
Your cloud services saga is now chronicled in 'services.json'.
Deciphering Cloud Service Scrolls
Reading and revealing the cloud service names is like reading from an ancient scroll:
# Read the scroll in 'services.json'
with open("services.json", "r") as json_file:
data = json.load(json_file)
# Unveil the services
for provider, service in data.items():
print(f"{provider} : {service}")
The magic output appears:
aws : ec2
azure : VM
gcp : compute engine
Python's Alchemy on YAML Chronicles
Transforming YAML into JSON is a gentle wave of the wand:
import yaml
import json
# Read the YAML spellbook
with open("services.yaml", "r") as yaml_file:
yaml_data = yaml.safe_load(yaml_file)
# Witness the seamless transformation
json_data = json.dumps(yaml_data, indent=2)
print(json_data)
And just like that, Python transmutes YAML into JSON effortlessly.
In the realm of DevOps, Python is your friendly guide, making JSON and YAML handling a piece of cake. Join the simplicity and make your DevOps journey a delightful experience! โจ๐
Happy coding! ๐ป๐