Skip to content

Instantly share code, notes, and snippets.

@LouisAmon
Created July 26, 2018 12:49
Show Gist options
  • Save LouisAmon/4bd79b8ab80d3851601f3f9016300ac4 to your computer and use it in GitHub Desktop.
Save LouisAmon/4bd79b8ab80d3851601f3f9016300ac4 to your computer and use it in GitHub Desktop.
How to compress JSON-able data using gzip (Python 3)
import json
import gzip
def compress_data(data):
# Convert to JSON
json_data = json.dumps(data, indent=2)
# Convert to bytes
encoded = json_data.encode('utf-8')
# Compress
compressed = gzip.compress(encoded)
@JohnAtl
Copy link

JohnAtl commented Mar 7, 2023

This was very helpful. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment