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.
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
- Documentation: https://jg-rp.github.io/python-jsonpath/.
- JSONPath Syntax: https://jg-rp.github.io/python-jsonpath/syntax/
- Change log: https://github.com/jg-rp/python-jsonpath/blob/main/CHANGELOG.md
- PyPi: https://pypi.org/project/python-jsonpath
- Source code: https://github.com/jg-rp/python-jsonpath
- Issue tracker: https://github.com/jg-rp/python-jsonpath/issues
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
Since version 0.8.0, 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
Since version 0.8.0, we also include an RFC 6902 compliant implementation of JSON Patch. See JSON Patch quick start and API reference
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'}}}
License
python-jsonpath
is distributed under the terms of the MIT license.
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
python_jsonpath-1.1.1.tar.gz
(40.4 kB
view hashes)
Built Distribution
Close
Hashes for python_jsonpath-1.1.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 43f2622b7aaaf4f45dd873e80cfd181058503e08ffdeac5218135f3a97bd0aec |
|
MD5 | 74b19db2d1f1b30c0fba918543ad5cd7 |
|
BLAKE2b-256 | e88ab4dbc2b00ad4ae24fe70f40207113dee10283d8beba278d9e6b4960b1a77 |