#90DaysOfDevOps
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:
Create a Dictionary in Python and write it to a JSON File.
The JSON module can be used to generate a dictionary in Python and write it to a JSON file:
json.dumps() : It is used to convert a dictionary to JSON string.
Read a JSON file
services.json
kept in this folder and print the service names of every cloud service provider.json.load() accepts a file object, parses the JSON data, fills a Python dictionary with the data, and returns it to you.
Read YAML file using python, file
services.yaml
and read the contents to convert yaml to json.First, use the import statement to import the yaml module.
Using the open method, read the file.
Method of the safeloader reads the contents of the file and converts it to a dictionary Python object
Convert yaml to json:
json.dumps() function will convert a subset of Python objects into a json string.
Thank you for taking the time to read this article. I hope you enjoy it. Please follow me for more devops-related blogs.
~Prabhakar Yadav