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.34.tar.gz
(8.3 kB
view details)
Built Distribution
File details
Details for the file jsonpatchext-1.34.tar.gz
.
File metadata
- Download URL: jsonpatchext-1.34.tar.gz
- Upload date:
- Size: 8.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.24.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.51.0 CPython/3.8.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | ba116ef5cc1a9783b02837ea517e0c0d9e7929aa524f50681bf5108f5e92add0 |
|
MD5 | ae478c9cdd1e5545836a5213ed65cd40 |
|
BLAKE2b-256 | 83bb08e6e675942aae0539c31c7c95a918b663d95d0a51141ee6493de83589b0 |
File details
Details for the file jsonpatchext-1.34-py2.py3-none-any.whl
.
File metadata
- Download URL: jsonpatchext-1.34-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.24.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.51.0 CPython/3.8.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | be9e4d04ae2eed7e86a9894912cf89f9475536e7232dfff1567b46517ae72838 |
|
MD5 | b1e62ff40c9f1be734e1fa73324a77ca |
|
BLAKE2b-256 | b37e4494a0cb5045a465f097c7409d463ff99a7a3122f23703b686b9af9dda9f |