Day 15 Task: Python Libraries for DevOps - Parsing JSON and YAML

Hey there! π οΈ On Day 15 of my DevOps journey, I explored the versatile world of Python libraries that are essential for any DevOps engineer. Today was all about parsing data - because let's face it, understanding and manipulating data is at the core of what we do.
Working with Data Files in Python π
Python is a treasure trove of libraries, and for us in DevOps, os, sys, json, and yaml are particularly invaluable. They make file manipulation and data parsing a breeze.
JSON in Python π
JSON (JavaScript Object Notation) is a lightweight data interchange format that's easy to read and write for humans and easy to parse and generate for machines.
Here's how I went about today's tasks with JSON:
Creating a Dictionary and Writing to a JSON File:
First, I defined my dictionary:
my_data = {'name': 'Davender', 'role': 'DevOps Engineer'}Next, I wrote it to a JSON file:
import json with open('data.json', 'w') as jsonfile: json.dump(my_data, jsonfile)And that's it! My data was safely stored in
data.json.
Reading a JSON File:
I had a
services.jsonfile with cloud service names, and my task was to print them out.Here's the code snippet I used:
with open('services.json', 'r') as jsonfile: services = json.load(jsonfile) for provider, service in services.items(): print(f"{provider} : {service}")The output was neat and exactly what I needed:
aws : ec2 azure : VM gcp : compute engine
YAML in Python π
YAML (YAML Ain't Markup Language) is another human-friendly data serialization standard for all programming languages. Reading YAML files in Python is just as straightforward once you have the PyYAML library installed.
Here's my approach to the YAML task:
Reading a YAML File:
First, I ensured
PyYAMLwas installed withpip install PyYAML.Then, I loaded the YAML file:
import yaml with open('services.yaml', 'r') as yamlfile: yaml_content = yaml.safe_load(yamlfile)Converting it to JSON was as simple as:
json_content = json.dumps(yaml_content) print(json_content)And voilΓ ! I had the YAML content in JSON format, ready to be used however I needed.
Wrapping Up π¬
Today was all about data parsing. Whether itβs JSON or YAML, understanding how to work with these formats is crucial in our field. Itβs tasks like these that reinforce my appreciation for Pythonβs simplicity and power. Stay tuned for more adventures in DevOps and Python! π #Day15 #DevOps #Python #JSON #YAML #DataParsing
Thank you for reading this Blog. Hope you learned something new today! If you found this blog helpful, please like, share, and follow me for more blog posts like this in the future.
You can connect with me at: https://www.linkedin.com/in/davendersingh/




