If you have a Python dictionary, and want to encode it as a string and zip it to save space, perhaps for passing a dictionary through as an environment variable or similar, then you can do the following

Zip then Encode / Decode then Unzip Functions

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
import json, gzip, base64
from io import BytesIO


def _zip_then_encode(data: dict) -> str:
    """Gzip and base64 encode a dictionary"""
    if type(data) != dict:
        raise TypeError("data must be a dictionary")
    compressed = BytesIO()
    with gzip.GzipFile(fileobj=compressed, mode="w") as f:
        json_response = json.dumps(data)
        f.write(json_response.encode("utf-8"))
    return base64.b64encode(compressed.getvalue()).decode("ascii")

def _decode_then_unzip(data) -> dict:
    res = base64.b64decode(data)
    res = gzip.decompress(res)
    res = res.decode("utf-8")
    res = json.loads(res)
    return res

To use the encode and decode functions, you can do the following:

Zip and Encode the Dictionary to String

1
2
3
4
5
6
7
8
my_dict = {
    'somekey': {
        'another': 'value'
    }
}

encoded_str = _zip_then_encode(my_dict)
print(encoded_str)

Output:

H4sIAM0O9mMC/6tWKs7PTc1OrVSyUqhWSszLL8lILQKylcoSc0pTlWprAUha5+ghAAAA

Decode and Unzip the String to Dictionary

Now you can take the string and reverse it back into the dictionary as follows:

1
print(_decode_then_unzip(encoded_str))

Output:

{'somekey': {'another': 'value'}}