Python Libraries for DevOps

Day 15 : 90Days of DevOps Challenge

ยท

3 min read

-> Reading JSON in Python

To read JSON files in Python, you can use the json module.

import json

# Open the JSON file
with open('data.json') as f:
    data = json.load(f)

# Access data in the JSON file
print(data['name'])

In this example, we open the file "data.json" using the open() function and read its contents using the json.load() function. The result is stored in the variable data, which is now a dictionary.

-> Reading YAML in Python

To read YAML files in Python, you can use the PyYAML module.

import yaml

# Open the YAML file
with open('data.yaml') as f:
    data = yaml.safe_load(f)

# Access data in the YAML file
print(data['name'])

In this example, we open the file "data.yaml" using the open() function and read its contents using the yaml.safe_load() function. The result is stored in the variable data, which is now a dictionary.

Note that we use yaml.safe_load() instead of yaml.load() to prevent arbitrary code execution when loading YAML files.

Task :

-> Create a Dictionary in Python and write it to a json File.

here's an example of how to create a dictionary in Python and write it to a JSON file:

import json

# Create a dictionary
my_dict = {
    "name": "John",
    "age": 30,
    "city": "New York"
}

# Write dictionary to a JSON file
with open("my_dict.json", "w") as outfile:
    json.dump(my_dict, outfile)

In this example, we import the json module and create a dictionary called my_dict with three key-value pairs.

Then, we use the open() function to create a file object with the name "my_dict.json" and the write mode "w". We then use the json.dump() function to write the dictionary to the file. The dump() function takes two arguments: the dictionary to be written, and the file object to write it to.

After running this code, you should have a new file called my_dict.json in your current working directory, containing the JSON representation of the dictionary.

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

Assuming that the services.json file contains a dictionary of cloud service providers and their corresponding services, here's an example of how to read the file and print out the service names of each cloud service provider:

import json

# Open the JSON file
with open('services.json') as f:
    data = json.load(f)

# Loop through the dictionary and print out the service names for each provider
for provider, services in data.items():
    print(provider, ":", ", ".join(services))

In this code, we open the file "services.json" using the open() function and read its contents using the json.load() function. The result is stored in the variable data, which is now a dictionary.

We then loop through the dictionary using the items() method and print out the provider name and a comma-separated list of its services using the join() method.

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

here's an example of how to read a YAML file in Python and convert it to JSON:

import yaml
import json

# Open the YAML file and read its contents
with open('services.yaml') as f:
    data = yaml.safe_load(f)

# Convert YAML to JSON
json_data = json.dumps(data)

# Print the JSON data
print(json_data)

In this example, we use the PyYAML module to read the YAML file "services.yaml" and load its contents into a Python object called data.

We then use the json.dumps() function to convert the data object to JSON format and store it in the variable json_data.

Finally, we print the json_data variable, which contains the JSON representation of the YAML file contents.

Thank You for Reading the Blog.. ๐Ÿ‘

Connect with me : linkedin.com/in/shivraj-salunkhe-5881141a4

Follow my blog channel : shivrajofficial.hashnode.dev

ย