compress_json

The missing Python utility to read and write compressed JSONs.
The library is loosely based on the compress_pickle library.
How do I install this package?
As usual, just download it using pip:
pip install compress_json
Available compression modes
The compression modes, detected automatically by the file name, are gzip, bz2, and lzma (or xz), with the notable exception of zip which seems difficult to integrate into the JSON pipeline.
Usage example
The library is extremely easy to use:
import compress_json
D = {
"A": {
"B": "C"
}
}
compress_json.dump(D, "filepath.json.gz")
compress_json.dump(D, "filepath.json.bz")
compress_json.dump(D, "filepath.json.lzma")
compress_json.dump(D, "filepath.json.xz")
D1 = compress_json.load("filepath.json.gz")
D2 = compress_json.load("filepath.json.bz")
D3 = compress_json.load("filepath.json.lzma")
D3 = compress_json.load("filepath.json.xz")
If it happens that you have to load or dump a JSON object with a custom extension, you can specify the compression mode by passing the compression parameter to the load and dump methods:
import compress_json
D = {
"A": {
"B": "C"
}
}
compress_json.dump(D, "filepath.custom_extension1", compression="gzip")
compress_json.dump(D, "filepath.custom_extension2", compression="bz2")
compress_json.dump(D, "filepath.custom_extension3", compression="lzma")
D1 = compress_json.load("filepath.custom_extension1", compression="gzip")
D2 = compress_json.load("filepath.custom_extension2", compression="bz2")
D3 = compress_json.load("filepath.custom_extension3", compression="lzma")
assert D == D1 == D2 == D3
Some extra perks: local loading and dumping
The library makes available, other than the usual load and dump from the JSON library, the methods local_load and local_dump, which let you load and dump files in the same directory as wherever you are calling them, by using the call stack.
This can be useful, especially when loading files within packages.
import compress_json
D = {
"A": {
"B": "C"
}
}
compress_json.local_dump(D, "filepath.json.gz")
compress_json.local_dump(D, "filepath.json.bz")
compress_json.local_dump(D, "filepath.json.lzma")
D1 = compress_json.local_load("filepath.json.gz")
D2 = compress_json.local_load("filepath.json.bz")
D3 = compress_json.local_load("filepath.json.lzma")
assert D == D1 == D2 == D3
Loading with RAM cache
Sometimes you need to load a compressed JSON file a LOT of times, and you may want to put this document in a cache or something of the sort. Fortunately, we already provide this option for you:
import compress_json
D1 = compress_json.load(
"filepath.json.gz",
use_cache=True
)
D2 = compress_json.local_load(
"filepath.json.gz",
use_cache=True
)
assert D1 == D2
Advanced usage
You can pass parameters to either the chosen compression mode or the JSON library.
With the json_kwargs parameter, you can specify any of the kwargs that should be forwarded to the JSON library method, which you can obtain for your Python version by running help(json.dump) and help(json.load), depending on whether you are dumping or loading the JSON object.
Similarly, with the compression_kwargs parameter, you can specify any parameter that has to be forwarded to the compression library that you intend to use, whether that is lzma, gzip, or bz2, and as per JSON will depend on which version you have installed.
Whether you are dumping or loading a compressed JSON object, you can get the list of parameters you have available to forward to the compression method by running help(lzma.open), help(gzip.open), or help(bz2.open), respectively.
import compress_json
D = {
"A": {
"B": "C"
}
}
compress_json.dump(
D, "filepath.json.gz",
compression_kwargs={
"compresslevel": 9
},
json_kwargs={
"indent": 4
}
)
D4 = compress_json.load(
"filepath.json.gz",
compression_kwargs={
"compresslevel": 9
},
json_kwargs={}
)
assert D == D4
License
The library is released under the MIT license.