Skip to main content

A small example package

Project description

pyjsonpatch

GitHub License GitHub branch check runs

About

A Python implementation of JSON Pointer (RFC 6902) and JSON Patch (RFC 6902). Primarily, the package can do the following to Python object(s) representing JSON(s):

  • apply_patch to modify an object with a JSON patch
  • generate_patch to generate a JSON Patch from two objects
  • get_by_ptr to retrieve a value from object using a JSON pointer

Table of Contents

Installation

Python 3.8 or higher is required. You can install the library with:

# Linux/macOS
python3 -m pip install -U pyjsonpatch

# Windows
py -3 -m pip install -U pyjsonpatch

Development

Install the dev requirements with:

# Linux/macOS
python3 -m pip install -r requirements-dev.txt

# Windows
py -3 -m pip install -r requirements-dev.txt

Linting and Formatting

Run the Ruff linter and formatter with:

# Lint
ruff check --fix

# Format
ruff format

Testing

Run tests with:

# Linux/macOS
python3 -m unittest discover tests

# Windows
py -3 -m unittest discover tests

Building and Releasing

Build with:

# Linux/macOS
python3 -m build

# Windows
py -3 -m build

Commit messages should follow Conventional Commits and version numbers follow Semantic Versioning. Releases require a version bump in pyproject.toml alongside a push to main with that version as a tag.

Examples

get_by_ptr

from pyjsonpatch import get_by_ptr


source = {"": 1, "foo": [2, 3]}

print(get_by_ptr(source, "").obj)
# {"": 1, "foo": [2, 3]}
print(get_by_ptr(source, "/").obj)
# 1
print(get_by_ptr(source, "/foo").obj)
# [2, 3]
print(get_by_ptr(source, "/foo/0").obj)
# 2

apply_patch

from pyjsonpatch import apply_patch


source = {"": 1, "foo": [2, 3]}
patch = [
  {"op": "add", "path": "/hello", "value": "world"},
  {"op": "add", "path": "/foo/1", "value": 4},
  {"op": "add", "path": "/foo/-", "value": 5},
  {"op": "remove", "path": "/"},
]
res = apply_patch(source, patch)

print(res.obj)
# {"foo": [2, 4, 3, 5], "hello": "world"}
print(res.obj is source)
# True
#  - source was mutated
print(res.removed)
# [None, None, None, 1]
#  - Only the 4th operation removes something

generate_patch

from pyjsonpatch import generate_patch


source = {"": 1, "foo": [2, 3]}
target = {"foo": [2, 4], "hello": "world"}
print(generate_patch(source, target))
# [
#   {"op": "remove": "path": "/"},
#   {"op": "replace": "path": "/foo/1", "value": 4},
#   {"op": "add": "path": "/hello", "value": "world"},
# ]

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

pyjsonpatch-0.1.1.tar.gz (15.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pyjsonpatch-0.1.1-py3-none-any.whl (13.9 kB view details)

Uploaded Python 3

File details

Details for the file pyjsonpatch-0.1.1.tar.gz.

File metadata

  • Download URL: pyjsonpatch-0.1.1.tar.gz
  • Upload date:
  • Size: 15.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.0.1 CPython/3.12.8

File hashes

Hashes for pyjsonpatch-0.1.1.tar.gz
Algorithm Hash digest
SHA256 7897cb108451fbcec3a02749460abbeb3bbad5285d9ad9aa7f81210d8e6cb029
MD5 e7765fb4427797a0e5629b9baa3152de
BLAKE2b-256 d9c7f2635f4ed5189f3d479e7172e015935a730fb0ce275376803c5b3fecce8b

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyjsonpatch-0.1.1.tar.gz:

Publisher: publish.yml on deephaven/pyjsonpatch

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pyjsonpatch-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: pyjsonpatch-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 13.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.0.1 CPython/3.12.8

File hashes

Hashes for pyjsonpatch-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 20ef5ca88f98dc0bb81947e713f50e6fd71a162230f23b56f2760f6e4057a971
MD5 a715b3fac84e8a54f204a37ffefa9dde
BLAKE2b-256 8b798fdc724a4d1efb92d457a581b7364b14a80b831429c1f7845a9d6251f371

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyjsonpatch-0.1.1-py3-none-any.whl:

Publisher: publish.yml on deephaven/pyjsonpatch

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

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