Working with JSON and YAML Files in Python

Working with JSON and YAML Files in Python


Introduction

Welcome back to my 90DaysOfDevOps challenge! On day 15, I delved into the world of Python libraries for handling JSON and YAML files. One of the tasks for the day involved creating a dictionary in Python and writing it to a JSON file, reading a JSON file to print service names of different cloud service providers, and finally, reading a YAML file to convert its contents into JSON format.

Let's dive into the details of each task.


Task 1: Creating a Dictionary and Writing to a JSON File

# Python code for creating a dictionary and writing it to a JSON file
import json

json_file_name="service.json" #jason file : which is going to have thisdict as string"
# Function for creating service.json file 
def python_dict_to_json(python_dict):
    with open(json_file_name,'w') as file:
       json.dump(python_dict, file)
       return "python_dict_to_json Successful"
thisdict = {
    "services": {
      "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
      }
    }
  }
result_1=python_dict_to_json(thisdict)
print(result_1)

This simple Python script creates a dictionary representing cloud services and writes it to a JSON file named services.json.


Task 2: Reading a JSON File and Printing Service Names

# Python code for reading a JSON file and printing service names
import json

with open("service.json", 'r') as file:
        data = json.load(file)
#print(data)

for key in data['services']:
    print(key,":",data['services'][key]['name'])

This script reads the services.json file and prints the service names of each cloud service provider. The output will look like:

aws : ec2
azure : VM
gcp : compute engine

Task 3: Reading a YAML File and Converting to JSON

# Python code for reading a YAML file and converting it to JSON

import yaml
import json

yaml_file_name="service.yml"  # yaml file : which data is going to  retrieve in python file
yaml_to_json="y2j.json" # json file : whuch is going to have yaml file data in it
# Function for Reading service.yml data
def yaml_to_python_dict(yaml_file_name):
    with open(yaml_file_name, 'r') as file:
      data=yaml.safe_load(file)
      return data

# function for converting service.yml to json file y2j.json
def yaml_to_json_(yaml_data):
    with open(yaml_to_json,'w') as file:
       json.dump(yaml_data, file)
       return "yaml_to_json Successful"
#calling Functions
data=yaml_to_python_dict(yaml_file_name)
print(data['services']['aws'])

result_2=yaml_to_json_(data)
print(result_2)

In this script, we use the PyYAML library to read a YAML file (services.yaml) and then convert its contents into JSON format. The resulting JSON data is printed to the console.

Conclusion

Day 15 of the 90DaysOfDevOps challenge was a productive exploration of Python libraries for working with JSON and YAML files. The code snippets provided here are just the beginning, and you can find additional resources and scripts in my GitHub repository. These tasks are fundamental in DevOps workflows, especially when dealing with configuration management and cloud service provisioning. Stay tuned for more updates as I continue my DevOps journey!Feel free to adapt the code snippets and explanations according to your preferences and context. Good luck with your DevOps journey!