Exploring Cloud Service Providers: Python 
          Dictionary and YAML to JSON Conversion

Exploring Cloud Service Providers: Python Dictionary and YAML to JSON Conversion

ยท

3 min read

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

Step 1: Create a dictionary in Python:

pythonCopy codedata = {
    "name": "John Doe",
    "age": 30,
    "city": "New York"
}

In this example, we have a dictionary named data with three key-value pairs.

Step 2: Import the json module:

pythonCopy codeimport json

The json module in Python provides methods for working with JSON data.

Step 3: Convert the dictionary to a JSON string:

pythonCopy codejson_data = json.dumps(data)

The json.dumps() function is used to convert the Python dictionary to a JSON string representation.

Step 4: Write the JSON string to a file:

pythonCopy codewith open('data.json', 'w') as file:
    file.write(json_data)

Here, we open a file named 'data.json' in write mode ('w') using a with statement. Then, we write the JSON string to the file using the write() method.

The complete code would look like this:

pythonCopy codeimport json

data = {
    "name": "John Doe",
    "age": 30,
    "city": "New York"
}

json_data = json.dumps(data)

with open('data.json', 'w') as file:
    file.write(json_data)

After executing this code, a file named 'data.json' will be created in the current directory, containing the JSON representation of the dictionary. You can customize the file path and name as needed.

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

Step 1: Install the PyYAML library if you haven't already. You can install it using pip:

Copy codepip install pyyaml

Step 2: Import the necessary modules:

pythonCopy codeimport yaml
import json

Step 3: Read the YAML file:

pythonCopy codewith open('data.yaml', 'r') as file:
    yaml_data = yaml.safe_load(file)

In this example, we assume that the YAML file is named 'data.yaml'. You can replace it with the appropriate file name or path.

Step 4: Convert the YAML data to JSON:

pythonCopy codejson_data = json.dumps(yaml_data)

The json.dumps() function is used to convert the YAML data to a JSON string.

Step 5: Write the JSON data to a file or perform any other desired operations:

pythonCopy codewith open('data.json', 'w') as file:
    file.write(json_data)

In this step, we open a file named 'data.json' in write mode ('w') using a with statement. Then, we write the JSON string to the file using the write() method. You can modify the file path and name according to your requirements.

The complete code would look like this:

pythonCopy codeimport yaml
import json

with open('data.yaml', 'r') as file:
    yaml_data = yaml.safe_load(file)

json_data = json.dumps(yaml_data)

with open('data.json', 'w') as file:
    file.write(json_data)

After running this code, a file named 'data.json' will be created, containing the JSON representation of the YAML data.

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.

ย