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 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
A JSON file is a file that contains data in JSON (JavaScript Object Notation) format. JSON files store structured data and are often used for configuration files, data exchange between different systems, and data storage. JSON files have the ".json" file extension. JSON files have a specific structure consisting of key-value pairs and can represent various data types, including strings, numbers, objects, arrays, booleans, and null values. JSON is human-readable, making it easy for humans and machines to work with.
json.dump() - In Python, json.dump() is a method from the json module used to serialize (encode) a Python object and write it to a file-like object in JSON format. It is often used to save Python data as JSON in a file or other output stream.
- Read a json file
services.json
kept in this folder and print the service names of every cloud service provider.Rear.d a json fileservices.json
kept in this folder and print the service names of every cloud service provide
output
aws: ec2
azure: VM
gcp: compute engine
json.loads()
is a Python method that is used to deserialize a JSON formatted string and convert it into a Python object. The term "loads" stands for "load string." This is commonly used to convert JSON data into a Python dictionary, list, or other appropriate data structure.
- Read YAML file using python, file
services.yaml
and read the contents to convert yaml to json
Thanks for reading!