Apply JSON-Patches (RFC 6902) with extensions
Project description
python-json-patch-ext
Applying JSON Patches in Python
This module extends the Python jsonpatch module to add 'check', 'mutate' and 'merge' operations.
See source code for examples
- Website: https://github.com/RangelReale/python-json-patch-ext
- Repository: https://github.com/RangelReale/python-json-patch-ext.git
- Documentation: https://python-json-patch-ext.readthedocs.org/
- PyPI: https://pypi.python.org/pypi/jsonpatchext
Example
from jsonpatchext import JsonPatchExt, JsonPatchTestFailed
def StartsWithComparator(current, compare):
if not current.startswith(compare):
msg = '{0} ({1}) does not starts with {2} ({3})'
raise JsonPatchTestFailed(msg.format(current, type(current), compare, type(compare)))
def RemoveLastMutator(current, value):
return current[:-1]
patch = JsonPatchExt([
{'op': 'add', 'path': '/foo', 'value': {'bar': 'bar'}},
{'op': 'check', 'path': '/foo/bar', 'value': 'bar', 'cmp': 'equals'},
{'op': 'merge', 'path': '/foo', 'value': {'newbar': 'newbarvalue'}},
{'op': 'check', 'path': '/foo/newbar', 'value': 'newb', 'cmp': 'custom', 'comparator': StartsWithComparator},
{'op': 'mutate', 'path': '/foo/newbar', 'mut': 'uppercase'},
{'op': 'mutate', 'path': '/foo/newbar', 'mut': 'custom', 'mutator': RemoveLastMutator},
{'op': 'mutate', 'path': '/foo/bar', 'mut': ['uppercase', ('custom', RemoveLastMutator)]},
])
doc = {}
result = patch.apply(doc)
print(result)
checkpatch = JsonPatchExt([
{'op': 'check', 'path': '/foo/bar', 'value': 'BA', 'cmp': 'equals'},
{'op': 'check', 'path': '/foo/newbar', 'value': 'NEWB', 'cmp': 'custom', 'comparator': StartsWithComparator},
])
result = checkpatch.check(result)
print(result)
Output:
{'foo': {'bar': 'BA', 'newbar': 'NEWBARVALU'}}
True
Author
Rangel Reale (rangelspam@gmail.com)
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
jsonpatchext-1.33.tar.gz
(8.2 kB
view details)
Built Distribution
File details
Details for the file jsonpatchext-1.33.tar.gz
.
File metadata
- Download URL: jsonpatchext-1.33.tar.gz
- Upload date:
- Size: 8.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.25.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.52.0 CPython/3.8.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 18010e1fe6433e65eb4a3f228bd4951f09eb1e642471a9463064cf96a01030a5 |
|
MD5 | 7d4fa521060b475c14c5099342d44fda |
|
BLAKE2b-256 | 8a355fa5c527b5421e0f1735c1c213dfba591734460bd106cd9cc2547c8fda18 |
File details
Details for the file jsonpatchext-1.33-py2.py3-none-any.whl
.
File metadata
- Download URL: jsonpatchext-1.33-py2.py3-none-any.whl
- Upload date:
- Size: 9.0 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.25.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.52.0 CPython/3.8.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 246581601d47212484bb6bc5fb2844b13ca02a553768f70c6c237677d0b7d48d |
|
MD5 | c79b01ae2d526164c5795a2c4c2171f5 |
|
BLAKE2b-256 | 5215fbf5000608df013c145ec06d6de043f2dc2096f1433a54e390e995ecc948 |