# Python Libraries for DevOps| Day 15 of 90 Days of DevOps

🚀 Welcome to Day 13 of #90DaysOfDevOps! 🎉

Today, let's dive into a crucial aspect of being a DevOps Engineer - reading JSON and YAML in Python. 🐍📚

📝 **Reading JSON and YAML in Python** As DevOps Engineers, parsing files is a fundamental skill. Python offers powerful libraries that simplify handling various file formats like txt, json, and yaml.

**Libraries for DevOps in Python** Python boasts a wealth of libraries like os, sys, json, and yaml, which prove indispensable in our day-to-day tasks.

* **Task 1: Create a Dictionary and Write to JSON** Let's start by creating a Python dictionary and writing it to a JSON file. JSON provides an organized and human-readable structure for data exchange.
    
    ```python
    import json
    
    # Create a Python dictionary
    data = {
        "name": "John Doe",
        "age": 30,
        "occupation": "DevOps Engineer"
    }
    
    # Write dictionary to JSON file
    with open("data.json", "w") as json_file:
        json.dump(data, json_file, indent=4)
    ```
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1691086592311/3911c6e1-f8fc-4956-a506-b23a2ff64ef7.png align="center")
    
* **Task 2: Read JSON and Extract Cloud Service Names** Now, we'll read a JSON file, "services.json," and extract the service names of each cloud service provider. The content of "services.json" is shown below:
    
    ```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
            }
        }
    }
    ```
    
    ```python
    # Read JSON and extract cloud service names
    with open("services.json", "r") as json_file:
        data = json.load(json_file)
    
    cloud_providers = data["services"].keys()
    print("Cloud Service Providers:")
    for provider in cloud_providers:
        if provider != "debug":
            print("- ", provider)
    ```
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1691086613290/d37e8ef2-0744-42e7-91b4-f52944d8cfc9.png align="center")
    
* **Task 3: Read YAML and Convert to JSON** YAML is another popular data serialization format. Let's read "services.yaml" and convert its contents to JSON. The content of "services.yaml" is shown below:
    
    ```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
    ```
    
    ```python
    import yaml
    
    # Read YAML and convert to JSON
    with open("services.yaml", "r") as yaml_file:
        data = yaml.safe_load(yaml_file)
    
    with open("services_converted.json", "w") as json_file:
        json.dump(data, json_file, indent=4)
    ```
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1691086632576/10515e69-12f2-4c7a-9e1d-aaa687aebfaf.png align="center")

🏁 **Conclusion:** Reading JSON and YAML files in Python is a fundamental skill for DevOps Engineers. With Python's powerful libraries, we can efficiently handle data and extract valuable information. Make sure to practice these tasks to enhance your DevOps toolkit! 💼🔧

Feel free to connect with me on [LinkedIn](https://www.linkedin.com/in/ajitfawade/) 🤝 Check out my [GitHub](https://github.com/ajitfawade) for more resources 📚

Happy learning, and stay tuned for more exciting DevOps content! 🚀🎉
