A Python implementation of JSON Pointer and JSON Patch
Project description
pyjsonpatch
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_patchto modify an object with a JSON patchgenerate_patchto generate a JSON Patch from two objectsget_by_ptrto 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
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pyjsonpatch-0.1.2.tar.gz.
File metadata
- Download URL: pyjsonpatch-0.1.2.tar.gz
- Upload date:
- Size: 15.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.0.1 CPython/3.12.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
914890265be7035d8f1553368be88a95865c26a836c45f4a130a2816ec977205
|
|
| MD5 |
fb6d6247ce2da2f81482bd74ba8fa7a2
|
|
| BLAKE2b-256 |
4d565d148cb4e8154aab84229576ad5636700b4276d8629cb08c6ccdf53e8ee6
|
Provenance
The following attestation bundles were made for pyjsonpatch-0.1.2.tar.gz:
Publisher:
publish.yml on deephaven/pyjsonpatch
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pyjsonpatch-0.1.2.tar.gz -
Subject digest:
914890265be7035d8f1553368be88a95865c26a836c45f4a130a2816ec977205 - Sigstore transparency entry: 156740363
- Sigstore integration time:
-
Permalink:
deephaven/pyjsonpatch@48594b21911fdf01df55760dd8afc95ee0a32762 -
Branch / Tag:
refs/tags/v0.1.2 - Owner: https://github.com/deephaven
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@48594b21911fdf01df55760dd8afc95ee0a32762 -
Trigger Event:
push
-
Statement type:
File details
Details for the file pyjsonpatch-0.1.2-py3-none-any.whl.
File metadata
- Download URL: pyjsonpatch-0.1.2-py3-none-any.whl
- Upload date:
- Size: 14.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.0.1 CPython/3.12.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b7d121efbcd76d5d377d70afd13d79c19a06711e0b251285793a3763d4530c5b
|
|
| MD5 |
98c077edae4e94742958693a522a3f27
|
|
| BLAKE2b-256 |
903c39d1ba78c15db5846d78d2b65aaa744461f4ab237bdd635771f854a27671
|
Provenance
The following attestation bundles were made for pyjsonpatch-0.1.2-py3-none-any.whl:
Publisher:
publish.yml on deephaven/pyjsonpatch
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pyjsonpatch-0.1.2-py3-none-any.whl -
Subject digest:
b7d121efbcd76d5d377d70afd13d79c19a06711e0b251285793a3763d4530c5b - Sigstore transparency entry: 156740365
- Sigstore integration time:
-
Permalink:
deephaven/pyjsonpatch@48594b21911fdf01df55760dd8afc95ee0a32762 -
Branch / Tag:
refs/tags/v0.1.2 - Owner: https://github.com/deephaven
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@48594b21911fdf01df55760dd8afc95ee0a32762 -
Trigger Event:
push
-
Statement type: