Day-15 Task: Python Libraries for DevOps

Day-15 Task: Python Libraries for DevOps

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

What is JSON?

The full form of JSON is JavaScript Object Notation. JSON is a lightweight data format for data interchange that can be easily read and written by humans, easily parsed, and generated by machines. It is a complete language-independent text format. To work with JSON data, Python has a built-in package called json. Json writes in a { } braces in the form of key:value separated by comma ,

What is YAML?

The full form of YAML is Yet Another Markup Language. It is a light-weight, human-readable data-representation language. It is primarily designed to make the format easy to read while including complex features. Since YAML is a superset of JSON, it can parse JSON with a YAML parser.The extensions in YAML are .yaml or .yml. YAML specifications allow user-defined data types as well as explicit data typing.

Tasks:

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

    Function Used:

    json.dump() Method can convert a Python object into a JSON File.

    Syntax: json.dump(dict, file_pointer)

    Parameters:

    • dictionary – name of dictionary which should be converted to JSON object.

    • file pointer – pointer of the file opened in write or append mode.

    • After writing dictionary into JSON file, a new .json file will create

  2. Create a Dictionary in Python and write it to a JSON String.

    Function Used:

    json.dumps() Method can convert a Python object into a JSON String.

    Syntax: json.dumps(dict, file_pointer)

    Parameters:

    • dictionary – name of dictionary which should be converted to JSON object.

    • indent – defines the number of units for indentation

  3. Read a JSON file services.json kept in this folder and print the service names of every cloud service provider.

    Function Used:

    json.load() Method accepts file object, parses the JSON data, populates a Python dictionary with the data, and returns it back to you.

    Syntax: json.load(fp)

    Parameters:

    • fp: File pointer to read text.

  4. Create a JSON String and convert it into Python Dictionary.

    Function Used:

    json.loads() Method accepts file object, parses the JSON data, populates a Python dictionary with the data and returns it back to you.

    Syntax: json.load(jsonstring)

    Parameters:

    • jsonstring: valid JSON string

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

    • We can read the YAML file using the PyYAML module’s yaml.safe_load() function in python dictionary form

    • We will store the data in a JSON file using json.dump

    • Then we will read the JSON file using json.load

    • To install the PyYAML module, you’ll need to run the pip command, which is the package installer for Python.The below command installs the PyYAML module.

      pip install pyyaml


Thank you for reading the article.

Thanks for your valuable time.

Happy Learning !... Keep Learning ! 😊