condense-json

raw JSON →
0.1.3 verified Mon Apr 27 auth: no python

A Python library for condensing JSON objects by replacing long values with shorter replacement strings, useful for reducing payload size or token count. Version 0.1.3, stable but early stage; no set release cadence.

pip install condense-json
error KeyError: None
cause Replacements dict contains None as a value, causing KeyError in versions <=0.1.1.
fix
Remove None values from replacements or upgrade to >=0.1.2.
error KeyError: ''
cause Replacements dict contains an empty string as a key, causing KeyError in versions <=0.1.2.
fix
Remove empty string keys from replacements or upgrade to >=0.1.3.
gotcha If replacements dict contains None values, condense_json may raise KeyError. Ensure all replacement values are strings or non-None.
fix Update to >=0.1.2 or ensure replacements dict has no None values.
gotcha If a replacement key is an empty string, condense_json may raise KeyError on blank replacements.
fix Update to >=0.1.3 or avoid using empty strings as replacement keys.
gotcha condense_json only replaces exact string matches; it does not recurse into nested objects' keys, only values.
fix Ensure replacements are exact value strings, not substrings. Use recursive calls if needed.

Basic usage: condense JSON values using a replacements dict, then restore.

from condense_json import condense_json, uncondense_json

obj = {"name": "John Doe", "email": "john@example.com", "long_field": "a very long string that we want to replace"}
replacements = {"a very long string that we want to replace": "short"}
condensed = condense_json(obj, replacements)
print(condensed)  # {"name": "John Doe", "email": "john@example.com", "long_field": "short"}
restored = uncondense_json(condensed, replacements)
print(restored)  # original obj