Skip to main content

Apply JSON-Patches (RFC 6902) with extensions

Project description

python-json-patch-ext

PyPI version Supported Python versions

Applying JSON Patches in Python

This module extends the Python jsonpatch module to add 'check', 'mutate' and 'merge' operations.

See source code for examples

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},
])
doc = {}
result = patch.apply(doc)
print(result)

checkpatch = JsonPatchExt([
    {'op': 'check', 'path': '/foo/bar', 'value': 'bar', 'cmp': 'equals'},
    {'op': 'check', 'path': '/foo/newbar', 'value': 'NEWB', 'cmp': 'custom', 'comparator': StartsWithComparator},
])

result = checkpatch.check(result)
print(result)

Output:

{'foo': {'bar': 'bar', 'newbar': 'NEWBARVALU'}}
True

Author

Rangel Reale (rangelspam@gmail.com)

Project details


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.39.tar.gz (8.5 kB view details)

Uploaded Source

Built Distribution

jsonpatchext-1.39-py2.py3-none-any.whl (9.2 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file jsonpatchext-1.39.tar.gz.

File metadata

  • Download URL: jsonpatchext-1.39.tar.gz
  • Upload date:
  • Size: 8.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.4.0 pkginfo/1.7.0 requests/2.25.0 requests-toolbelt/0.9.1 tqdm/4.61.0 CPython/3.8.6

File hashes

Hashes for jsonpatchext-1.39.tar.gz
Algorithm Hash digest
SHA256 5881f33d409139dd92e348b3bec4b377b4495958479ff517e4b39c5bead1bbee
MD5 9adb8320d48af4846f1d8d29e96a9ad7
BLAKE2b-256 db8789e44561a7fe4e3bc6086e670381e4f9db9687f815037295bb70d53f61ed

See more details on using hashes here.

File details

Details for the file jsonpatchext-1.39-py2.py3-none-any.whl.

File metadata

  • Download URL: jsonpatchext-1.39-py2.py3-none-any.whl
  • Upload date:
  • Size: 9.2 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.4.0 pkginfo/1.7.0 requests/2.25.0 requests-toolbelt/0.9.1 tqdm/4.61.0 CPython/3.8.6

File hashes

Hashes for jsonpatchext-1.39-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 60a22d0517b8d5540c7956bf9119f1da9d52a1285e56448e20047bbd3944200a
MD5 0baafd7d6b834218c6fe91acfbff1548
BLAKE2b-256 f4e1fd7d2865d813d46298daf4e5087ed6dfcc05f512150dfc80b6fd6a0e8846

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page