import bz2, json, contextlib
hugeData = {'key': {'x': 1, 'y': 2}}
with contextlib.closing(bz2.BZ2File('data.json.bz2', 'wb')) as f:
    json.dump(hugeData, f)
Fastest way to store large files in Python