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

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

ยท

3 min read

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:

  1. 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.

  2. Reading a JSON File:

    • I had a services.json file 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:

  1. Reading a YAML File:

    • First, I ensured PyYAML was installed with pip 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/

ย