Skip to main content

Python function for condensing JSON using replacement strings

Project description

condense-json

PyPI Tests Changelog License

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 same replacements dictionary 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

condense_json-0.1.2.tar.gz (8.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

condense_json-0.1.2-py3-none-any.whl (8.5 kB view details)

Uploaded Python 3

File details

Details for the file condense_json-0.1.2.tar.gz.

File metadata

  • Download URL: condense_json-0.1.2.tar.gz
  • Upload date:
  • Size: 8.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for condense_json-0.1.2.tar.gz
Algorithm Hash digest
SHA256 a161e1984122ad35801cc14ecfe2b38d9d638c8c089e04cb545d26300fdc485e
MD5 d3a1a4fa14b686e154abb885d7711a12
BLAKE2b-256 cac7fb14dfac23e6e27170f8a94bf13355987d15f8db62df99146f81bf6b2508

See more details on using hashes here.

Provenance

The following attestation bundles were made for condense_json-0.1.2.tar.gz:

Publisher: publish.yml on simonw/condense-json

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file condense_json-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: condense_json-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 8.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for condense_json-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 bedc4f9de5c626483e57577adfebb9ef655a3b8ad15ab85401e2fdcc27aa37bb
MD5 02d539ee3bf760727ccba62e1eb028e3
BLAKE2b-256 883c51cf297b9fe99577a5b1c16ca6a38787bf65555455a2991135c510dfc189

See more details on using hashes here.

Provenance

The following attestation bundles were made for condense_json-0.1.2-py3-none-any.whl:

Publisher: publish.yml on simonw/condense-json

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page