Skip to main content

Command Palette

Search for a command to run...

Day 15 : Python Libraries for DevOps

Published
3 min readView as Markdown
Day 15 : Python Libraries for DevOps
  • As a DevOps Engineer, you should be able to parse files, be it txt, json, yaml, etc.

JSON (JavaScript Object Notation) and YAML (YAML Ain’t Markup Language) are both commonly used formats for storing and exchanging data in Python.

JSON is a lightweight data-interchange format that is easy for humans to read and write and easy for machines to parse and generate.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.

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.

Tasks

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

  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

we have used the open() function to read the JSON file. Then, the file is parsed using json.load() method which gives a dictionary named json_data.

Then just print the json_data [From the output can see].

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

    In this example, we first open the services.yaml file in read mode ("r") using open(). We then use yaml.safe_load() from the pyyaml module to load the YAML data from the file into the yaml_data variable.

    Next, we convert the yaml_data to JSON format using json.dumps(), which returns a JSON-formatted string representation of the data.

    Finally, we print the json_data, which contains the converted JSON data.

    Thanks for reading this article...