Day 15 Task: Python Libraries for DevOps

Day 15 Task: Python Libraries for DevOps

#90daysdevopschallenge

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 is a lightweight data-interchange format that is easy for humans to read and write and easy for machines to parse and generate. It is based on a subset of the JavaScript Programming Language. JSON is a text format that is completely language-independent but uses conventions that are familiar to programmers of the C family of languages, including C, C++, C#, Java, JavaScript, Perl, Python, etc. These properties make JSON an ideal data-interchange language.

  • JSON is a standard format for data exchange, which is often used for exchanging data between a server and a web application. In Python, the json module provides functions for working with JSON in Python. This module provides two methods for encoding Python objects into JSON format, json.dump() and json.dumps(), and two methods for decoding JSON, json.load() and json.loads()

  • YAML, on the other hand, is a human-readable data serialization format. It is often used for configuration files and data exchange between languages that have different data structures. YAML is a superset of JSON and can be used to represent complex data structures such as lists and dictionaries. It is also less verbose than JSON, making it easier to read and write.

  • Both JSON and YAML can be easily read and written in Python using built-in libraries such as json and yaml. For example, to read a JSON file in Python, you can use the json.load() method, and to write a JSON file, you can use the json.dump() method. Similarly, to read a YAML file in Python, you can use the yaml.load() method, and to write a YAML file, you can use the yaml.dump() method.

  • In summary, both JSON and YAML are popular formats for storing and exchanging data in Python. JSON is a lightweight, easy-to-parse data interchange format, while YAML is a human-readable data serialization format that is often used for configuration files. Both can be easily read and written in Python using built-in libraries.

    Task 1 :Create a Dictionary in Python and write it to a JSON File.

    To create a dictionary in Python and write it to a JSON file, you can use the JSON library. Here is an example:

    json.dump() → convert a Python object into an equivalent JSON object and store the result in a JSON file in the working directory.

    Task 2 :Read a json file services.json kept in this folder and print the service names of every cloud service provider.

      output
      aws : ec2
      azure : VM
      gcp : compute engine
    

    Here, we to do this task, we need to understand how to parse and access data objects in a JSON file. The most effective way to do this is by trying it on your own. Once you get the idea, we can read this JSON file and load into a data object. From this, we can easily parse through and use loops to get the desired output. Give it a try!!

    json.loads() → used to parse a valid JSON string and convert it into a Python Dictionary.

    Task3:Read YAML file using python, file services.yaml and read the contents to convert yaml to json

    YAML file to JSON conversion can easily be done by loading the YAML file first into the python object and then converting the python object into its equivalent JSON format using the appropriate functions.

    THANK YOU FOR READING :)