Day 14/90DaysofDevOps Challenge- Python Libraries for DevOps
Today, let’s dive into a crucial aspect of being a DevOps Engineer, reading JSON and YAML in Python.
Python is a versatile programming language
?? JSON in Python :
import json
# JSON to Python object
json_data = '{"name": "John", "age": 30}'
python_obj = json.loads(json_data)
print(python_obj)
# Output: {'name': 'John', 'age': 30}
# Python object to JSON
python_obj = {'name': 'John', 'age': 30}
json_data = json.dumps(python_obj)
print(json_data)
# Output: {"name": "John", "age": 30}
?? YAML in Python:
import yaml
# YAML to Python object
yaml_data = '''
name: John
age: 30
# Python object to YAML
python_obj = {'name': 'John', 'age': 30}
yaml_data = yaml.dump(python_obj)
print(yaml_data)
# Output: "age: 30 name: John"
Tasks1:
Create a Dictionary in Python and write it to a JSON File.
领英推荐
TASK-2
Read a json file services.json kept in this folder and print the service names of every cloud service provider.
{
"services": {
"aws": {
"name": "EC2"
},
"azure": {
"name": "VM"
},
"gcp": {
"name": "Compute Engine"
}
}
}
Make sure that the file ‘services.json’ is in the same folder as the python file you created, otherwise, you’ll get a file not found error when trying to read the json file.
TASK3:
Read YAML file using python, file services.yaml and read the contents to convert yaml to json :
YAML is another popular data serialization format
---
services:
aws:
name: EC2
azure:
name: VM
gcp:
name: Compute Engine
Thank you for reading. please do share and click the like?? button below to show your support.
Happy Learning??!!!