Python Libraries for DevOps

Python Libraries for DevOps

#day15

#90daysdevopschallenge

What is JSON?

JSON stands for "JavaScript Object Notation". It is a widely used data interchange format that is language-agnostic, meaning it can be used in various programming languages including Python and JAVA.

JSON in Python:

Python has a built-in module called JSON. It provides a function for working with JSON data.

Serialization (Python to JSON):

To convert a Python data structure such as a list & dictionary into a JSON string, use json.dumps() function.

import json as js

data = {"name" : "Tom","age": 20}

json_str= js.dumps(data) print(json_str)

Deserialization (JSON to Python):

To convert JSON string to Python data structure, use json.load() function.

import json as js

json_str = '{"name":"Tom","age":20}'

data=js.loads(json_str) print(data["name"])

What is YAML?

YAML stands for "Yet Another Markup Language". It is human human-readable data serialization language. It is often used for configuration but also used for data exchange.

YAML in Python:

The most commonly used Python YAML Parser is PyYAML. It is a library that allows you to load, parse, and write YAML.

Python converts into YAML:

import yaml

data = {"name":"Tom","age":20,"city":"NewYork"}

yaml_data = yaml.dump(data,default_flow_style = False)

YAML convert into Python:

python_data = yaml.safe.load(yaml_data)

print(f"name: {python_data["name"]}")

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

    service.json:

    { "aws": { "name": "ec2" }, "azure": { "name": "VM" }, "gcp": { "name": "compute engine" } }

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

    In next article, will start our Docker journey....

    Thank you for giving your precious time to read this blog/article and if any suggestions or improvements are required on my blogs feel free to connect on Linkedin Unnati Gupta. Happy Learning !!!