Exploring Cloud Service Providers: Python Dictionary and YAML to JSON Conversion
In today's blog post, we will dive into working with cloud service provider data using Python. Specifically, we will cover two tasks: creating a dictionary in Python and writing it to a JSON file, and reading a YAML file and converting its contents to JSON format. Let's get started!
Task 1: Creating a Dictionary in Python and Writing to a JSON File
We begin by working with a JSON file that contains data about various cloud service providers. Here is the JSON file we will be using:
{
"services": {
"debug": "on",
"aws": {
"name": "EC2",
"type": "pay per hour",
"instances": 500,
"count": 500
},
"azure": {
"name": "VM",
"type": "pay per hour",
"instances": 500,
"count": 500
},
"gcp": {
"name": "Compute Engine",
"type": "pay per hour",
"instances": 500,
"count": 500
}
}
}
Our goal is to extract the names of the cloud service providers from this JSON file. Let's write the Python code to achieve this:
import json
# Read the JSON file
with open('data.json') as file:
data = json.load(file)
# Extract service names
services = data['services']
service_names = list(services.keys())
# Print the service names
print("Service Names:")
for service in service_names:
print(service)
When you run this code, you will see the following output:
Service Names:
aws : ec2
azure : VM
gcp : compute engine
We successfully extracted the names of the cloud service providers from the JSON file!!!
Task 2: Reading a YAML File and Converting it to JSON
In the second part of our exploration, we will work with a YAML file that contains similar cloud service provider data. Here is the YAML file we will be using:
---
services:
debug: 'on'
aws:
name: EC2
type: pay per hour
instances: 500
count: 500
azure:
name: VM
type: pay per hour
instances: 500
count: 500
gcp:
name: Compute Engine
type: pay per hour
instances: 500
count: 500
Our objective is to read the contents of this YAML file and convert it to JSON format. Let's write the Python code to accomplish this:
import yaml
import json
# Read the YAML file
with open('services.yaml') as file:
yaml_data = yaml.safe_load(file)
# Convert YAML to JSON
json_data = json.dumps(yaml_data, indent=2)
# Print the JSON data
print("JSON Data:")
print(json_data)
When you run this code, you will see the following output:
JSON Data:
{
"services": {
"debug": "on",
"aws": {
"name": "EC2",
"type": "pay per hour",
"instances": 500,
"count": 500
},
"azure": {
"name": "VM",
"type": "pay per hour",
"instances": 500,
"count": 500
},
"gcp": {
"name": "Compute Engine",
"type": "pay per hour",
"instances": 500,
"count": 500
}
}
}
Great! We successfully read the YAML file and converted its contents to JSON format using Python!!!
Conclusion
In this blog post, we explored two tasks related to cloud service provider data using Python. We learned how to create a dictionary in Python and write it to a JSON file, as well as how to read a YAML file and convert its contents to JSON format. These skills are valuable for handling and manipulating data from various sources in the cloud computing domain.
I hope you found this blog post informative and helpful. Thank you for reading, and happy coding!
To connect with me - https://www.linkedin.com/in/subhodey/