#day 15 Task #90 Days of Devops Challenge #train with shubam

#day 15 Task #90 Days of Devops Challenge #train with shubam

Python Libraries for DevOps

Creating a Dictionary in Python and writing it to json file

import json
# Sample dictionary

cloud_providers = {'Google': "GCP", 'Amazon': "AWS", 'Micrososft': "Azure"}

# Convert dictionary to JSON string
data = json.dumps(cloud_providers)

# Print JSON string
print(data)

# Open a file for writing
with open("cloud_providers.json", "w") as f:
    # Write the dictionary to the file
    f.write(data)
    f.close()

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

import json
#reading json file
json_file = open('F:/VS CODE/day15_task/Devops-Challenge/2023/day15/services.json', 'r')
data = json_file.read()
#parsing it into dictionary
services = json.loads(data)
#converting keys into list
key_list = list(services['services'].keys())
#printing key value using list
for i in range(1,len(key_list)):
    print(key_list[i],":",services['services'][key_list[i]]['name'])

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

import yaml
import json
#opening yaml file
input_file = open('F:/VS CODE/day15_task/Devops-Challenge/2023/day15/services.yaml','r')
yaml_data = input_file.read()
#parsing yaml file to dict
yaml_file = yaml.safe_load(yaml_data)
print(type(yaml_file))
#parsing dict to json
json_file = json.dumps(yaml_file)
#writing json file as output
with open("new_services.json",'w') as f:
    f.write(json_file)
    f.close()