jsonpath-rfc9535


Namejsonpath-rfc9535 JSON
Version 0.1.3 PyPI version JSON
download
home_pageNone
SummaryRFC 9535 - JSONPath: Query Expressions for JSON in Python
upload_time2024-08-05 06:47:27
maintainerNone
docs_urlNone
authorNone
requires_python>=3.8
licenseNone
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            <h1 align="center">RFC 9535 JSONPath: Query Expressions for JSON in Python</h1>

<p align="center">
We follow <a href="https://datatracker.ietf.org/doc/html/rfc9535">RFC 9535</a> strictly and test against the <a href="https://github.com/jsonpath-standard/jsonpath-compliance-test-suite">JSONPath Compliance Test Suite</a>.
</p>

<p align="center">
  <a href="https://github.com/jg-rp/python-jsonpath-rfc9535/blob/main/LICENSE.txt">
    <img src="https://img.shields.io/pypi/l/jsonpath-rfc9535.svg?style=flat-square" alt="License">
  </a>
  <a href="https://github.com/jg-rp/python-jsonpath-rfc9535/actions">
    <img src="https://img.shields.io/github/actions/workflow/status/jg-rp/python-jsonpath-rfc9535/tests.yaml?branch=main&label=tests&style=flat-square" alt="Tests">
  </a>
  <br>
  <a href="https://pypi.org/project/jsonpath-rfc9535">
    <img src="https://img.shields.io/pypi/v/jsonpath-rfc9535.svg?style=flat-square" alt="PyPi - Version">
  </a>
  <a href="https://pypi.org/project/jsonpath-rfc9535">
    <img src="https://img.shields.io/pypi/pyversions/jsonpath-rfc9535.svg?style=flat-square" alt="Python versions">
  </a>
</p>

---

**Table of Contents**

- [Install](#install)
- [Example](#example)
- [Links](#links)
- [Related projects](#related-projects)
- [API](#api)
- [License](#license)

## Install

Install Python JSONPath RFC 9535 using [pip](https://pip.pypa.io/en/stable/getting-started/):

```
pip install jsonpath-rfc9535
```

Or [Pipenv](https://pipenv.pypa.io/en/latest/):

```
pipenv install -u jsonpath-rfc9535
```

## Example

```python
import jsonpath_rfc9535 as jsonpath

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

for node in jsonpath.find("$.users[?@.score > 85]", data):
    print(node.value)

# {'name': 'Sue', 'score': 100}
# {'name': 'John', 'score': 86, 'admin': True}
```

Or, reading JSON data from a file:

```python
import json
import jsonpath_rfc9535 as jsonpath

with open("/path/to/some.json", encoding="utf-8") as fd:
    data = json.load(fd)

nodes = jsonpath.find("$.some.query", data)
values = nodes.values()
# ...
```

You could read data from a YAML formatted file too. If you have [PyYaml](https://pyyaml.org/wiki/PyYAML) installed:

```python
import jsonpath_rfc9535 as jsonpath
import yaml

with open("some.yaml") as fd:
    data = yaml.safe_load(fd)

products = jsonpath.find("$..products.*", data).values()
# ...
```

## Links

- Change log: https://github.com/jg-rp/python-jsonpath-rfc9535/blob/main/CHANGELOG.md
- PyPi: https://pypi.org/project/jsonpath-rfc9535
- Source code: https://github.com/jg-rp/python-jsonpath-rfc9535
- Issue tracker: https://github.com/jg-rp/python-jsonpath-rfc9535/issues

## Related projects

- [Python JSONPath](https://github.com/jg-rp/python-jsonpath) - Another Python package implementing JSONPath, but with additional features and customization options.
- [JSON P3](https://github.com/jg-rp/json-p3) - RFC 9535 implemented in TypeScript.

## API

### find

`find(query: str, value: JSONValue) -> JSONPathNodeList`

Apply JSONPath expression _query_ to _value_. _value_ should arbitrary, possible nested, Python dictionaries, lists, strings, integers, floats, Booleans or `None`, as you would get from [`json.load()`](https://docs.python.org/3/library/json.html#json.load).

A list of `JSONPathNode` instances is returned, one node for each value matched by _path_. The returned list will be empty if there were no matches.

Each `JSONPathNode` has:

- a `value` property, which is the JSON-like value associated with the node.
- a `location` property, which is a tuple of property names and array/list indexes that were required to reach the node's value in the target JSON document.
- a `path()` method, which returns the normalized path to the node in the target JSON document.

```python
import jsonpath_rfc9535 as jsonpath

value = {
    "users": [
        {"name": "Sue", "score": 100},
        {"name": "John", "score": 86, "admin": True},
        {"name": "Sally", "score": 84, "admin": False},
        {"name": "Jane", "score": 55},
    ],
    "moderator": "John",
}

for node in jsonpath.find("$.users[?@.score > 85]", value):
    print(f"{node.value} at '{node.path()}'")

# {'name': 'Sue', 'score': 100} at '$['users'][0]'
# {'name': 'John', 'score': 86, 'admin': True} at '$['users'][1]'
```

`JSONPathNodeList` is a subclass of `list` with some helper methods.

- `values()` returns a list of values, one for each node.
- `items()` returns a list of `(normalized path, value)` tuples.

### find_one

`find_one(query: str, value: JSONValue) -> Optional[JSONPathNode]`

`find_one()` accepts the same arguments as [`find()`](#findquery-value), but returns the first available `JSONPathNode`, or `None` if there were no matches.

`find_one()` is equivalent to:

```python
def find_one(query, value):
    try:
        return next(iter(jsonpath.finditer(query, value)))
    except StopIteration:
        return None
```

### finditer

`finditer(query: str, value: JSONValue) -> Iterable[JSONPathNode]`

`finditer()` accepts the same arguments as [`find()`](#findquery-value), but returns an iterator over `JSONPathNode` instances rather than a list. This could be useful if you're expecting a large number of results that you don't want to load into memory all at once.

### compile

`compile(query: str) -> JSONPathQuery`

`find(query, value)` is a convenience function for `JSONPathEnvironment().compile(query).apply(value)`. Use `compile(query)` to obtain a `JSONPathQuery` instance which can be applied to difference JSON-like values repeatedly.

```python
import jsonpath_rfc9535 as jsonpath

value = {
    "users": [
        {"name": "Sue", "score": 100},
        {"name": "John", "score": 86, "admin": True},
        {"name": "Sally", "score": 84, "admin": False},
        {"name": "Jane", "score": 55},
    ],
    "moderator": "John",
}

query = jsonpath.compile("$.users[?@.score > 85]")

for node in query.apply(value):
    print(f"{node.value} at '{node.path()}'")

# {'name': 'Sue', 'score': 100} at '$['users'][0]'
# {'name': 'John', 'score': 86, 'admin': True} at '$['users'][1]'
```

A `JSONPathQuery` has a `finditer(value)` method too, and `find(value)` is an alias for `apply(value)`.

## License

`python-jsonpath-rfc9535` is distributed under the terms of the [MIT](https://spdx.org/licenses/MIT.html) license.

            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "jsonpath-rfc9535",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.8",
    "maintainer_email": null,
    "keywords": null,
    "author": null,
    "author_email": "James Prior <jamesgr.prior@gmail.com>",
    "download_url": "https://files.pythonhosted.org/packages/8e/e6/313d94d5f7d4ada29dbc31a0a833857f97ac6e225e807b91911c7488338b/jsonpath_rfc9535-0.1.3.tar.gz",
    "platform": null,
    "description": "<h1 align=\"center\">RFC 9535 JSONPath: Query Expressions for JSON in Python</h1>\n\n<p align=\"center\">\nWe follow <a href=\"https://datatracker.ietf.org/doc/html/rfc9535\">RFC 9535</a> strictly and test against the <a href=\"https://github.com/jsonpath-standard/jsonpath-compliance-test-suite\">JSONPath Compliance Test Suite</a>.\n</p>\n\n<p align=\"center\">\n  <a href=\"https://github.com/jg-rp/python-jsonpath-rfc9535/blob/main/LICENSE.txt\">\n    <img src=\"https://img.shields.io/pypi/l/jsonpath-rfc9535.svg?style=flat-square\" alt=\"License\">\n  </a>\n  <a href=\"https://github.com/jg-rp/python-jsonpath-rfc9535/actions\">\n    <img src=\"https://img.shields.io/github/actions/workflow/status/jg-rp/python-jsonpath-rfc9535/tests.yaml?branch=main&label=tests&style=flat-square\" alt=\"Tests\">\n  </a>\n  <br>\n  <a href=\"https://pypi.org/project/jsonpath-rfc9535\">\n    <img src=\"https://img.shields.io/pypi/v/jsonpath-rfc9535.svg?style=flat-square\" alt=\"PyPi - Version\">\n  </a>\n  <a href=\"https://pypi.org/project/jsonpath-rfc9535\">\n    <img src=\"https://img.shields.io/pypi/pyversions/jsonpath-rfc9535.svg?style=flat-square\" alt=\"Python versions\">\n  </a>\n</p>\n\n---\n\n**Table of Contents**\n\n- [Install](#install)\n- [Example](#example)\n- [Links](#links)\n- [Related projects](#related-projects)\n- [API](#api)\n- [License](#license)\n\n## Install\n\nInstall Python JSONPath RFC 9535 using [pip](https://pip.pypa.io/en/stable/getting-started/):\n\n```\npip install jsonpath-rfc9535\n```\n\nOr [Pipenv](https://pipenv.pypa.io/en/latest/):\n\n```\npipenv install -u jsonpath-rfc9535\n```\n\n## Example\n\n```python\nimport jsonpath_rfc9535 as jsonpath\n\ndata = {\n    \"users\": [\n        {\"name\": \"Sue\", \"score\": 100},\n        {\"name\": \"Sally\", \"score\": 84, \"admin\": False},\n        {\"name\": \"John\", \"score\": 86, \"admin\": True},\n        {\"name\": \"Jane\", \"score\": 55},\n    ],\n    \"moderator\": \"John\",\n}\n\nfor node in jsonpath.find(\"$.users[?@.score > 85]\", data):\n    print(node.value)\n\n# {'name': 'Sue', 'score': 100}\n# {'name': 'John', 'score': 86, 'admin': True}\n```\n\nOr, reading JSON data from a file:\n\n```python\nimport json\nimport jsonpath_rfc9535 as jsonpath\n\nwith open(\"/path/to/some.json\", encoding=\"utf-8\") as fd:\n    data = json.load(fd)\n\nnodes = jsonpath.find(\"$.some.query\", data)\nvalues = nodes.values()\n# ...\n```\n\nYou could read data from a YAML formatted file too. If you have [PyYaml](https://pyyaml.org/wiki/PyYAML) installed:\n\n```python\nimport jsonpath_rfc9535 as jsonpath\nimport yaml\n\nwith open(\"some.yaml\") as fd:\n    data = yaml.safe_load(fd)\n\nproducts = jsonpath.find(\"$..products.*\", data).values()\n# ...\n```\n\n## Links\n\n- Change log: https://github.com/jg-rp/python-jsonpath-rfc9535/blob/main/CHANGELOG.md\n- PyPi: https://pypi.org/project/jsonpath-rfc9535\n- Source code: https://github.com/jg-rp/python-jsonpath-rfc9535\n- Issue tracker: https://github.com/jg-rp/python-jsonpath-rfc9535/issues\n\n## Related projects\n\n- [Python JSONPath](https://github.com/jg-rp/python-jsonpath) - Another Python package implementing JSONPath, but with additional features and customization options.\n- [JSON P3](https://github.com/jg-rp/json-p3) - RFC 9535 implemented in TypeScript.\n\n## API\n\n### find\n\n`find(query: str, value: JSONValue) -> JSONPathNodeList`\n\nApply JSONPath expression _query_ to _value_. _value_ should arbitrary, possible nested, Python dictionaries, lists, strings, integers, floats, Booleans or `None`, as you would get from [`json.load()`](https://docs.python.org/3/library/json.html#json.load).\n\nA list of `JSONPathNode` instances is returned, one node for each value matched by _path_. The returned list will be empty if there were no matches.\n\nEach `JSONPathNode` has:\n\n- a `value` property, which is the JSON-like value associated with the node.\n- a `location` property, which is a tuple of property names and array/list indexes that were required to reach the node's value in the target JSON document.\n- a `path()` method, which returns the normalized path to the node in the target JSON document.\n\n```python\nimport jsonpath_rfc9535 as jsonpath\n\nvalue = {\n    \"users\": [\n        {\"name\": \"Sue\", \"score\": 100},\n        {\"name\": \"John\", \"score\": 86, \"admin\": True},\n        {\"name\": \"Sally\", \"score\": 84, \"admin\": False},\n        {\"name\": \"Jane\", \"score\": 55},\n    ],\n    \"moderator\": \"John\",\n}\n\nfor node in jsonpath.find(\"$.users[?@.score > 85]\", value):\n    print(f\"{node.value} at '{node.path()}'\")\n\n# {'name': 'Sue', 'score': 100} at '$['users'][0]'\n# {'name': 'John', 'score': 86, 'admin': True} at '$['users'][1]'\n```\n\n`JSONPathNodeList` is a subclass of `list` with some helper methods.\n\n- `values()` returns a list of values, one for each node.\n- `items()` returns a list of `(normalized path, value)` tuples.\n\n### find_one\n\n`find_one(query: str, value: JSONValue) -> Optional[JSONPathNode]`\n\n`find_one()` accepts the same arguments as [`find()`](#findquery-value), but returns the first available `JSONPathNode`, or `None` if there were no matches.\n\n`find_one()` is equivalent to:\n\n```python\ndef find_one(query, value):\n    try:\n        return next(iter(jsonpath.finditer(query, value)))\n    except StopIteration:\n        return None\n```\n\n### finditer\n\n`finditer(query: str, value: JSONValue) -> Iterable[JSONPathNode]`\n\n`finditer()` accepts the same arguments as [`find()`](#findquery-value), but returns an iterator over `JSONPathNode` instances rather than a list. This could be useful if you're expecting a large number of results that you don't want to load into memory all at once.\n\n### compile\n\n`compile(query: str) -> JSONPathQuery`\n\n`find(query, value)` is a convenience function for `JSONPathEnvironment().compile(query).apply(value)`. Use `compile(query)` to obtain a `JSONPathQuery` instance which can be applied to difference JSON-like values repeatedly.\n\n```python\nimport jsonpath_rfc9535 as jsonpath\n\nvalue = {\n    \"users\": [\n        {\"name\": \"Sue\", \"score\": 100},\n        {\"name\": \"John\", \"score\": 86, \"admin\": True},\n        {\"name\": \"Sally\", \"score\": 84, \"admin\": False},\n        {\"name\": \"Jane\", \"score\": 55},\n    ],\n    \"moderator\": \"John\",\n}\n\nquery = jsonpath.compile(\"$.users[?@.score > 85]\")\n\nfor node in query.apply(value):\n    print(f\"{node.value} at '{node.path()}'\")\n\n# {'name': 'Sue', 'score': 100} at '$['users'][0]'\n# {'name': 'John', 'score': 86, 'admin': True} at '$['users'][1]'\n```\n\nA `JSONPathQuery` has a `finditer(value)` method too, and `find(value)` is an alias for `apply(value)`.\n\n## License\n\n`python-jsonpath-rfc9535` is distributed under the terms of the [MIT](https://spdx.org/licenses/MIT.html) license.\n",
    "bugtrack_url": null,
    "license": null,
    "summary": "RFC 9535 - JSONPath: Query Expressions for JSON in Python",
    "version": "0.1.3",
    "project_urls": {
        "Documentation": "https://jg-rp.github.io/python-jsonpath-rfc9535/",
        "Issues": "https://github.com/jg-rp/python-jsonpath-rfc9535/issues",
        "Source": "https://github.com/jg-rp/python-jsonpath-rfc9535"
    },
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "36e0fc5634309641ef592604746452ecdb53bd52cf48eb962fc043d0c47f8b30",
                "md5": "5e1fd096b0355cfd36f2228051d45b74",
                "sha256": "05f9014bb8cbbe28bf65f3ff3062b7a126965f6e5e17f9575a475aad9dadfba2"
            },
            "downloads": -1,
            "filename": "jsonpath_rfc9535-0.1.3-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "5e1fd096b0355cfd36f2228051d45b74",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.8",
            "size": 34906,
            "upload_time": "2024-08-05T06:47:25",
            "upload_time_iso_8601": "2024-08-05T06:47:25.447878Z",
            "url": "https://files.pythonhosted.org/packages/36/e0/fc5634309641ef592604746452ecdb53bd52cf48eb962fc043d0c47f8b30/jsonpath_rfc9535-0.1.3-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "8ee6313d94d5f7d4ada29dbc31a0a833857f97ac6e225e807b91911c7488338b",
                "md5": "6fa4db0183059095176fe13cdc66a7dc",
                "sha256": "2ce27ff4b857c8de8664e6d5528c7f7869ac1575d74f2562510d1d2bcbb1b5fc"
            },
            "downloads": -1,
            "filename": "jsonpath_rfc9535-0.1.3.tar.gz",
            "has_sig": false,
            "md5_digest": "6fa4db0183059095176fe13cdc66a7dc",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.8",
            "size": 26015,
            "upload_time": "2024-08-05T06:47:27",
            "upload_time_iso_8601": "2024-08-05T06:47:27.544923Z",
            "url": "https://files.pythonhosted.org/packages/8e/e6/313d94d5f7d4ada29dbc31a0a833857f97ac6e225e807b91911c7488338b/jsonpath_rfc9535-0.1.3.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-08-05 06:47:27",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "jg-rp",
    "github_project": "python-jsonpath-rfc9535",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "jsonpath-rfc9535"
}
        
Elapsed time: 3.63557s