Python function for condensing JSON using replacement strings
Project description
condense-json
Python function for condensing JSON using replacement strings
Installation
Install this library using pip:
pip install condense-json
Usage
The condense_json function searches a JSON-like object for strings that contain specified replacement substrings. It replaces these substrings with a compact representation, making the JSON more concise. The uncondense_json function reverses this process.
condense_json(obj: Dict, replacements: Dict[str, str]) -> Any
obj: The JSON-like object (nested dictionaries, lists, and strings) to condense.replacements: A dictionary where keys are replacement IDs (e.g., "1", "2") and values are the strings they represent.
The function returns a modified version of the input obj where matching substrings are replaced. If a string consists entirely of a replacement string, it's replaced with {"$": replacement_id}. If a string contains one or more replacement strings, it's replaced with {"$r": [ ...segments...]} where segments are the parts of the original string and replacement IDs.
Example:
from condense_json import condense_json
input_json = {
"foo": {
"bar": {
"string": "This is a string with foxes in it",
"nested": {
"more": ["Here is a string", "another with foxes in it too"]
},
}
}
}
replacements = {"1": "with foxes in it"}
condensed_output = condense_json(input_json, replacements)
print(condensed_output)
# Expected output:
# {
# "foo": {
# "bar": {
# "string": {"$r": ["This is a string ", {"$": "1"}]},
# "nested": {
# "more": [
# "Here is a string",
# {"$r": ["another ", {"$": "1"}, " too"]}
# ]
# }
# }
# }
# }
uncondense_json(obj: Dict, replacements: Dict[str, str]) -> Any
obj: The condensed JSON-like object.replacements: The samereplacementsdictionary used for condensing.
This function reverses the condense_json operation. It finds the {"$": replacement_id} and {"$r": [ ...segments...]} structures and replaces them with the original strings from the replacements dictionary.
Example:
from condense_json import uncondense_json, condense_json # Import both
original = {
"sentence": "The quick brown fox jumps over the lazy dog",
"nested": {"list": ["fast fox", "lazy dog", "just some text"]},
}
replacements = {"1": "quick brown fox", "2": "lazy dog"}
condensed = condense_json(original, replacements)
uncondensed = uncondense_json(condensed, replacements)
assert uncondensed == original
If the input obj to uncondense_json doesn't contain any condensed structures, it returns the input unchanged.
Development
To contribute to this library, first checkout the code. Then create a new virtual environment:
cd condense-json
python -m venv venv
source venv/bin/activate
Now install the dependencies and test dependencies:
python -m pip install -e '.[test]'
To run the tests:
python -m pytest
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file condense_json-0.1.3.tar.gz.
File metadata
- Download URL: condense_json-0.1.3.tar.gz
- Upload date:
- Size: 8.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
25fe8d434fdafd849e8d98f21a3e18f96ae2d6dbc2c17565f29e4843d039d2bc
|
|
| MD5 |
7aa8d952df092e8863daac2246cc51aa
|
|
| BLAKE2b-256 |
94b3d784cbc05556192ea1e798cae96363835d649fe7420ff030190789645be1
|
Provenance
The following attestation bundles were made for condense_json-0.1.3.tar.gz:
Publisher:
publish.yml on simonw/condense-json
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
condense_json-0.1.3.tar.gz -
Subject digest:
25fe8d434fdafd849e8d98f21a3e18f96ae2d6dbc2c17565f29e4843d039d2bc - Sigstore transparency entry: 212291166
- Sigstore integration time:
-
Permalink:
simonw/condense-json@b941a090e3d141663d0b04af6dfbceb1230bfd1c -
Branch / Tag:
refs/tags/0.1.3 - Owner: https://github.com/simonw
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@b941a090e3d141663d0b04af6dfbceb1230bfd1c -
Trigger Event:
release
-
Statement type:
File details
Details for the file condense_json-0.1.3-py3-none-any.whl.
File metadata
- Download URL: condense_json-0.1.3-py3-none-any.whl
- Upload date:
- Size: 8.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e0a3d42db4f44a89e74af8737d8e517e97420be0f7e5437087f4decfd38c3366
|
|
| MD5 |
cbfd2ba27841588a5d8c7facb56ad9e9
|
|
| BLAKE2b-256 |
285f63badd4924358fad1efa6defd66eef700ccf8783c0e44098987f867e8b1f
|
Provenance
The following attestation bundles were made for condense_json-0.1.3-py3-none-any.whl:
Publisher:
publish.yml on simonw/condense-json
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
condense_json-0.1.3-py3-none-any.whl -
Subject digest:
e0a3d42db4f44a89e74af8737d8e517e97420be0f7e5437087f4decfd38c3366 - Sigstore transparency entry: 212291168
- Sigstore integration time:
-
Permalink:
simonw/condense-json@b941a090e3d141663d0b04af6dfbceb1230bfd1c -
Branch / Tag:
refs/tags/0.1.3 - Owner: https://github.com/simonw
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@b941a090e3d141663d0b04af6dfbceb1230bfd1c -
Trigger Event:
release
-
Statement type: