Day - 15 : 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.

Tasks

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 module:

json.dumps() : It is used to convert a dictionary to JSON string.

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

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

3)Read the 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.load() function. This function parse and converts a YAML object to a Python dictionary (dict object).

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

Convert yaml to json:

Did you find this article valuable?

Support Sahil Kamble's blog by becoming a sponsor. Any amount is appreciated!