
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,YAMLetc. 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
jsonmodule to work with JSON data.Created the dictionary with the name
datawith some elements in it.Specifies the name of the JSON file as
file_name.Use the
open ()function with modewto write in a file.Inside the block, use
json.dump()to write thedatadictionary 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
jsonmodule to work with JSON data.json.load()is used to load the JSON data from the file.Retrieve the
servicesdictionary from the loaded JSON data.cloud_services.jsonis the name of your JSON file.Iterate through each service provider in the
servicesdictionary 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>

About the author
"Passionate and aspiring DevOps Engineer with a flair for automation and a hunger to optimize the software delivery lifecycle. As a recent graduate in Computer Science, I'm excited to dive into the world of DevOps, combining my coding skills with a deep interest in infrastructure management and continuous integration. With a knack for problem-solving, I'm on a mission to streamline processes, enhance efficiency, and drive collaboration across development and operations teams. Join me as I embark on this journey of learning, experimenting, and contributing to the evolving landscape of DevOps."
Comments
Join the discussionNo comments yet. Be the first to comment.
90daysofDevOps
"Unlock DevOps mastery in 90 days. Join the journey! 🔥 #DevOps #90DaysOfDevOps #TechSkills"




