Table of contents
Reading JSON and YAML in Python
As a DevOps Engineer, you should be able to parse files, be it Txt, JSON, YAML, etc.
You should know what libraries one should use in Python for DevOps.
Python has numerous libraries like
os
,sys
,JSON
,YAML
etc. that a DevOps Engineer uses in day-to-day tasks.JSON and YAML files define and provision infrastructure components programmatically and enable automated infrastructure management.
Configuration data in these files helps in managing deployments, release parameters and environment-specific settings.
These files contain pipeline definitions, allowing for configuration and modification of the CI/CD pipeline.
Hands-On Tasks
- Create a Dictionary in Python and write it to a JSON File.
Steps:
import json
data = {
"name": "John",
"age": 30,
"city": "New York"
}
file_name = "data.json"
with open(file_name, "w") as json_file:
json.dump(data, json_file)
print(f"Data has been written to {file_name}")
We import the
json
module to work with JSON data.Created the dictionary with the name
data
with some elements in it.Specifies the name of the JSON file as
file_name
.Use the
open ()
function with modew
to write in a file.Inside the block, use
json.dump()
to write thedata
dictionary to the JSON file.In the end, we print the message to indicate the file written in JSON file.
- Read the services.json file and print the service names of every cloud service provider.
services.json :
{
"services": {
"debug": "on",
"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
}
}
}
Steps:
You can use Python to read JSON files and print the service names of cloud providers.
import json
with open('cloud_services.json', 'r') as json_file:
data = json.load(json_file)
services = data.get('services', {})
for provider, service_info in services.items():
if isinstance(service_info, dict) and 'name' in service_info:
print(f"Service name for {provider}: {service_info['name']}")
Import the
json
module to work with JSON data.json.load()
is used to load the JSON data from the file.Retrieve the
services
dictionary from the loaded JSON data.cloud_services.json
is the name of your JSON file.Iterate through each service provider in the
services
dictionary and print the name of the service.- Read the services.yaml file using Python, and read the contents to convert YAML to JSON
services.yaml
services:
debug: 'on'
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
To read a YAML file in Python and convert it to JSON, you can use the PyYAML
library to handle the YAML file and json.dump()
to convert it to a JSON file.
import yaml
import json
with open('services.yaml', 'r') as yaml_file:
yaml_data = yaml.safe_load(yaml_file)
json_data = json.dumps(yaml_data, indent=2)
print(json_data)
Use of
yaml.safe_load()
function to read the YAML file in the Python data.Use
json.dumps()
function to convert the data into JSON data.The indent=2 is just for pretty printing.
Finally, we print the JSON data.
Make sure that you have the PyYAML library installed. You can install it using a pip e.g. pip install pyyaml
< That is all for today. Tomorrow will be a new topic. FOLLOW to join me in the journey of DevOps>