Name | condense-json JSON |
Version |
0.1.1
JSON |
| download |
home_page | None |
Summary | Python function for condensing JSON using replacement strings |
upload_time | 2025-02-18 21:08:10 |
maintainer | None |
docs_url | None |
author | Simon Willison |
requires_python | >=3.8 |
license | Apache-2.0 |
keywords |
|
VCS |
 |
bugtrack_url |
|
requirements |
No requirements were recorded.
|
Travis-CI |
No Travis.
|
coveralls test coverage |
No coveralls.
|
# condense-json
[](https://pypi.org/project/condense-json/)
[](https://github.com/simonw/condense-json/actions/workflows/test.yml)
[](https://github.com/simonw/condense-json/releases)
[](https://github.com/simonw/condense-json/blob/main/LICENSE)
Python function for condensing JSON using replacement strings
## Installation
Install this library using `pip`:
```bash
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:**
```python
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:**
```python
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:
```bash
cd condense-json
python -m venv venv
source venv/bin/activate
```
Now install the dependencies and test dependencies:
```bash
python -m pip install -e '.[test]'
```
To run the tests:
```bash
python -m pytest
```
Raw data
{
"_id": null,
"home_page": null,
"name": "condense-json",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.8",
"maintainer_email": null,
"keywords": null,
"author": "Simon Willison",
"author_email": null,
"download_url": "https://files.pythonhosted.org/packages/43/55/c8f425948bcf6e096305d68d80e266359f6cdbc8b7ef485085d06e49a34c/condense_json-0.1.1.tar.gz",
"platform": null,
"description": "# condense-json\n\n[](https://pypi.org/project/condense-json/)\n[](https://github.com/simonw/condense-json/actions/workflows/test.yml)\n[](https://github.com/simonw/condense-json/releases)\n[](https://github.com/simonw/condense-json/blob/main/LICENSE)\n\nPython function for condensing JSON using replacement strings\n\n## Installation\n\nInstall this library using `pip`:\n```bash\npip install condense-json\n```\n## Usage\n\nThe `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.\n\n**`condense_json(obj: Dict, replacements: Dict[str, str]) -> Any`**\n\n* **`obj`**: The JSON-like object (nested dictionaries, lists, and strings) to condense.\n* **`replacements`**: A dictionary where keys are replacement IDs (e.g., \"1\", \"2\") and values are the strings they represent.\n\nThe 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.\n\n**Example:**\n\n```python\nfrom condense_json import condense_json\n\ninput_json = {\n \"foo\": {\n \"bar\": {\n \"string\": \"This is a string with foxes in it\",\n \"nested\": {\n \"more\": [\"Here is a string\", \"another with foxes in it too\"]\n },\n }\n }\n}\n\nreplacements = {\"1\": \"with foxes in it\"}\n\ncondensed_output = condense_json(input_json, replacements)\nprint(condensed_output)\n# Expected output:\n# {\n# \"foo\": {\n# \"bar\": {\n# \"string\": {\"$r\": [\"This is a string \", {\"$\": \"1\"}]},\n# \"nested\": {\n# \"more\": [\n# \"Here is a string\",\n# {\"$r\": [\"another \", {\"$\": \"1\"}, \" too\"]}\n# ]\n# }\n# }\n# }\n# }\n\n```\n\n**`uncondense_json(obj: Dict, replacements: Dict[str, str]) -> Any`**\n\n* **`obj`**: The condensed JSON-like object.\n* **`replacements`**: The same `replacements` dictionary used for condensing.\n\nThis 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.\n\n**Example:**\n\n```python\nfrom condense_json import uncondense_json, condense_json # Import both\n\noriginal = {\n \"sentence\": \"The quick brown fox jumps over the lazy dog\",\n \"nested\": {\"list\": [\"fast fox\", \"lazy dog\", \"just some text\"]},\n}\nreplacements = {\"1\": \"quick brown fox\", \"2\": \"lazy dog\"}\ncondensed = condense_json(original, replacements)\nuncondensed = uncondense_json(condensed, replacements)\nassert uncondensed == original\n\n```\nIf the input `obj` to `uncondense_json` doesn't contain any condensed structures, it returns the input unchanged.\n\n## Development\n\nTo contribute to this library, first checkout the code. Then create a new virtual environment:\n```bash\ncd condense-json\npython -m venv venv\nsource venv/bin/activate\n```\nNow install the dependencies and test dependencies:\n```bash\npython -m pip install -e '.[test]'\n```\nTo run the tests:\n```bash\npython -m pytest\n```\n",
"bugtrack_url": null,
"license": "Apache-2.0",
"summary": "Python function for condensing JSON using replacement strings",
"version": "0.1.1",
"project_urls": {
"CI": "https://github.com/simonw/condense-json/actions",
"Changelog": "https://github.com/simonw/condense-json/releases",
"Homepage": "https://github.com/simonw/condense-json",
"Issues": "https://github.com/simonw/condense-json/issues"
},
"split_keywords": [],
"urls": [
{
"comment_text": null,
"digests": {
"blake2b_256": "09a6db453e178d2565947011d4a8712c2f28a75692cfee7c6f899521657d2fea",
"md5": "86a189d15251f6d79c36210dc906e876",
"sha256": "b70ae75f1370d151be0033e1285e2fa39e77aeebd4f1cb12bb626ce962dd22ce"
},
"downloads": -1,
"filename": "condense_json-0.1.1-py3-none-any.whl",
"has_sig": false,
"md5_digest": "86a189d15251f6d79c36210dc906e876",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.8",
"size": 8228,
"upload_time": "2025-02-18T21:08:09",
"upload_time_iso_8601": "2025-02-18T21:08:09.446215Z",
"url": "https://files.pythonhosted.org/packages/09/a6/db453e178d2565947011d4a8712c2f28a75692cfee7c6f899521657d2fea/condense_json-0.1.1-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": null,
"digests": {
"blake2b_256": "4355c8f425948bcf6e096305d68d80e266359f6cdbc8b7ef485085d06e49a34c",
"md5": "cbad83e034ed41b6e3d0c8341ba10fc7",
"sha256": "1c46f6216121e07328799517bb35275aabefadbcbb27b2a78bafac6c1de5f5dd"
},
"downloads": -1,
"filename": "condense_json-0.1.1.tar.gz",
"has_sig": false,
"md5_digest": "cbad83e034ed41b6e3d0c8341ba10fc7",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.8",
"size": 8437,
"upload_time": "2025-02-18T21:08:10",
"upload_time_iso_8601": "2025-02-18T21:08:10.677045Z",
"url": "https://files.pythonhosted.org/packages/43/55/c8f425948bcf6e096305d68d80e266359f6cdbc8b7ef485085d06e49a34c/condense_json-0.1.1.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2025-02-18 21:08:10",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "simonw",
"github_project": "condense-json",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"lcname": "condense-json"
}