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

Uploaded Source

Built Distribution

jsonpatchext-1.38-py2.py3-none-any.whl (9.1 kB view details)

Uploaded Python 2 Python 3

File details

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

File metadata

  • Download URL: jsonpatchext-1.38.tar.gz
  • Upload date:
  • Size: 8.4 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

Hashes for jsonpatchext-1.38.tar.gz
Algorithm Hash digest
SHA256 f86d8703a246f13c8f08173c489dffe49cdb41615859e109e4b38a48f4dc290c
MD5 b8af58759937cedb7995cf5f9c4c336a
BLAKE2b-256 6d4e7ac7cdfbf5aefec206d905481f49e8f29b1ffd5eb834ee6c0de6fa12639e

See more details on using hashes here.

File details

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

File metadata

  • Download URL: jsonpatchext-1.38-py2.py3-none-any.whl
  • Upload date:
  • Size: 9.1 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

Hashes for jsonpatchext-1.38-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 dbb0cbcda214cdf7f67c74fc9d614e4e526306e8f999a250fcbc0fd2beefecc5
MD5 7b6197806b3e78ffb37ace45daa5c962
BLAKE2b-256 789d0a9133903281be0969ffba985b183b9b7b84396ce8d989a5eedb0e3dfe82

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