Day 15 Task: Python Libraries for DevOps
What is JSON
JSON stands for JavaScript Object Notation , is a popular data format used for representing structured data. It's common to transmit and receive data between a server and web application in JSON format. In Python, JSON exists as a string. For example: p = '{"name": "Bob", "languages": ["Python", "Java"]}'
Example of JSON Syntax
{
"name": "John Smith",
"age": 35,
"email": "john@example.com",
"phone_numbers": [
{
"type": "home",
"number": "555-1234"
}
]
}
Tasks:
Create a Dictionary in Python and write it to a JSON File.
import json my_dict = { "name": "John", "age": 30, "city": "New York" } with open("my_dict.json", "w") as file: json.dump(my_dict, file)
Read a JSON file
services.json
kept in this folder and print the service names of every cloud service provider.Open your Python file (
cloud_services.py
) in your text editor.Add the following code to your Python file to read the
services.json
file and print the service names of every cloud service provider:
import json
with open('services.json') as f:
data = json.load(f)
for provider in data:
print(provider['name'], ':', provider['services'][0]['name'])
save your Python file (
cloud_services.py
).Open a command prompt or terminal window and navigate to the directory where your Python file and
services.json
file are saved.Run the Python file by typing the following command and pressing Enter:
python cloud_services.py
- The output should be displayed in the command prompt or terminal window:
yamlCopy codeaws : ec2
azure : VM
gcp : compute engine
- That's it! You have successfully read the
services.json
file and printed the service names of every cloud service provider using Python.
[
{
"name": "aws",
"services": "ec2"
},
{
"name": "azure",
"services": "VM"
},
{
"name": "gcp",
"services": "compute engine"
}
]
import json
# Read the JSON data from the file
with open('services.json') as f:
data = json.load(f)
# Print the service names for each cloud service provider
for provider in data:
print(provider['name'], ':', provider['services'])
Read YAML file using Python, file
services.yaml
and read the contents to convert yaml to jsonTo read a YAML file using Python and convert it to JSON, you can use the PyYAML and JSON modules. Here's an example of how to do it:
COPY
pythonCopy codeimport yaml import json # Open the YAML file and read its contents with open('services.yaml', 'r') as f: yaml_data = yaml.safe_load(f) # Convert the YAML data to JSON json_data = json.dumps(yaml_data) # Print the JSON data print(json_data)
In this example, we first open the
services.yaml
file in read mode using awith
block, which automatically closes the file when we're done with it. We then use theyaml.safe
_load()
method to parse the YAML data into a Python object.Next, we use the
json.dumps()
method to convert the Python object to JSON format, which can be used for further processing. Finally, we print the JSON data to the console using theprint()
function.Note that the
yaml.safe
_load()
method is used instead ofyaml.load()
for security reasons, as it only allows loading of simple Python objects (e.g. strings, lists, dictionaries), and does not