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.35.tar.gz
(8.3 kB
view hashes)
Built Distribution
Close
Hashes for jsonpatchext-1.35-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | de5770090f7ea018c2e4f2e118dc0a2ebee8a069ea1f0eedfd66a615ee78dfc0 |
|
MD5 | c905b0de329729b8ebdf23859493acab |
|
BLAKE2b-256 | 804abeca44893c40add853b20f67929dde8533d6599cbe10f3f1ce3c7f71dc21 |