Skip to main content

JSONPath, JSON Pointer and JSON Patch for Python.

Project description

Python JSONPath

A flexible JSONPath engine for Python.
We follow RFC 9535 and test against the JSONPath Compliance Test Suite.

License Tests PyPI - Downloads
PyPi - Version Python versions


Table of Contents

Install

Install Python JSONPath using pip:

pip install python-jsonpath

Or Pipenv:

pipenv install -u python-jsonpath

Or from conda-forge:

conda install -c conda-forge python-jsonpath

Links

Related projects

  • JSONPath RFC 9535 - A minimal, slightly cleaner Python implementation of RFC 9535. If you're not interested JSONPath sytax beyond that defined in RFC 9535, you might choose jsonpath-rfc9535 over python-jsonpath.

    jsonpath-rfc9535 also includes utilities for verifying and testing the JSONPath Compliance Test Suite. Most notably the nondeterministic behavior of some JSONPath selectors.

  • JSON P3 - RFC 9535 implemented in TypeScript. JSON P3 does not include all the non-standard features of Python JSONPath, but does define some optional extra syntax.

  • Ruby JSON P3 - RFC 9535, RFC 6901 and RFC 6902 implemented in Ruby.

Examples

JSONPath

import jsonpath

data = {
    "users": [
        {"name": "Sue", "score": 100},
        {"name": "John", "score": 86},
        {"name": "Sally", "score": 84},
        {"name": "Jane", "score": 55},
    ]
}

user_names = jsonpath.findall("$.users[?@.score < 100].name", data)
print(user_names) # ['John', 'Sally', 'Jane']

JSON Pointer

We include an RFC 6901 compliant implementation of JSON Pointer. See JSON Pointer quick start, guide and API reference

from jsonpath import pointer

data = {
    "users": [
        {"name": "Sue", "score": 100},
        {"name": "John", "score": 86},
        {"name": "Sally", "score": 84},
        {"name": "Jane", "score": 55},
    ]
}

sue_score = pointer.resolve("/users/0/score", data)
print(sue_score)  # 100

jane_score = pointer.resolve(["users", 3, "score"], data)
print(jane_score)  # 55

JSON Patch

We also include an RFC 6902 compliant implementation of JSON Patch. See JSON Patch quick start and the API reference.

[!WARNING] Objects passed to patch.apply() and JSONPatch.apply() are modified in place, even if a patch operation fails. Use patch.atomic() or JSONPatch.atomic() if you need to preserve input data on patch failure.

from jsonpath import patch

patch_operations = [
    {"op": "add", "path": "/some/foo", "value": {"foo": {}}},
    {"op": "add", "path": "/some/foo", "value": {"bar": []}},
    {"op": "copy", "from": "/some/other", "path": "/some/foo/else"},
    {"op": "add", "path": "/some/foo/bar/-", "value": 1},
]

data = {"some": {"other": "thing"}}
patch.apply(patch_operations, data)
print(data) # {'some': {'other': 'thing', 'foo': {'bar': [1], 'else': 'thing'}}}

Use patch.atomic() or JSONPatch.atomic() if you need to preserve input data on patch failure.

import contextlib

from jsonpath import JSONPatchError
from jsonpath import patch

patch_operations = [
    {"op": "add", "path": "/some/foo", "value": {"foo": {}}},
    {"op": "add", "path": "/some/foo", "value": {"bar": []}},
    {"op": "copy", "from": "/some/other", "path": "/some/foo/else"},
    {"op": "add", "path": "/some/foo/bar/-", "value": 1},
    {"op": "test", "path": "/some/thing", "value": "baz"},  # Always fails
]

data = {"some": {"other": "thing"}}

with contextlib.suppress(JSONPatchError):
    patch.atomic(patch_operations, data)

assert data == {"some": {"other": "thing"}}

License

python-jsonpath is distributed under the terms of the MIT license.

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

python_jsonpath-2.1.0.tar.gz (50.1 kB view details)

Uploaded Source

Built Distribution

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

python_jsonpath-2.1.0-py3-none-any.whl (64.5 kB view details)

Uploaded Python 3

File details

Details for the file python_jsonpath-2.1.0.tar.gz.

File metadata

  • Download URL: python_jsonpath-2.1.0.tar.gz
  • Upload date:
  • Size: 50.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.2

File hashes

Hashes for python_jsonpath-2.1.0.tar.gz
Algorithm Hash digest
SHA256 c94d9ea0aba7e2f69140ad2e379e8b1b75d66f6e20e82a7496393e5cb40b2572
MD5 eea253fef21a9ccc44afd2d102dc5896
BLAKE2b-256 bcaa07ec5da5a4549078bdea5935c03cccfbe58fefc6c82d0d9467655f6a7d0d

See more details on using hashes here.

File details

Details for the file python_jsonpath-2.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for python_jsonpath-2.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f5be070ccb0c9b8d3776cf0058905dc030ad6fcb5f9b6c3ad5c2f7f76a283451
MD5 7464ffd8e326bdef8b62a5bf94393993
BLAKE2b-256 d1307161a7384f02c61675e92dbd8915aa06e57ba2cb1a0049ed92f62f5be58d

See more details on using hashes here.

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