Skip to main content

Another JSONPath implementation for Python.

Project description

Python JSONPath

PyPI - Version PyPI - Python Version


Table of Contents

A flexible JSONPath engine for Python.

JSONPath is a mini language for extracting objects from data formatted in JavaScript Object Notation, or equivalent Python objects like dictionaries, and lists.

import jsonpath

data = {
    "categories": [
        {
            "name": "footwear",
            "products": [
                {
                    "title": "Trainers",
                    "description": "Fashionable trainers.",
                    "price": 89.99,
                },
                {
                    "title": "Barefoot Trainers",
                    "description": "Running trainers.",
                    "price": 130.00,
                },
            ],
        },
        {
            "name": "headwear",
            "products": [
                {
                    "title": "Cap",
                    "description": "Baseball cap",
                    "price": 15.00,
                },
                {
                    "title": "Beanie",
                    "description": "Winter running hat.",
                    "price": 9.00,
                },
            ],
        },
    ],
    "price_cap": 10,
}

products = jsonpath.findall("$..products.*", data)
print(products)

Install

Install Python Liquid using Pipenv:

pipenv install -u python-jsonpath

or pip:

pip install python-jsonpath

API

jsonpath.findall

findall(path: str, data: Sequence | Mapping) -> list[object]

Find all objects in data matching the given JSONPath path. If data is a string, it will be loaded using json.loads() and the default JSONDecoder.

Returns a list of matched objects, or an empty list if there were not matches.

jsonpath.finditer

finditer(path: str, data: Sequence | Mapping) -> iterable[JSONPathMatch]

Return an iterator yielding a JSONPathMatch instance for each match of the path in the given data. If data is a string, it will be loaded using json.loads() and the default JSONDecoder.

jsonpath.compile

compile(path: str) -> JSONPath | CompoundJSONPath

Prepare a path for repeated matching against different data. jsonpath.findall() and jsonpath.finditer() are convenience functions that call compile() for you.

JSONPath and CompoundJSONPath both have findall() and finditer() methods that behave the same as jsonpath.findall() and jsonpath.finditer(), just without taking a path string argument.

async

findall_async() and finditer_async are async equivalents of findall() and finditer(). They are used by when integrating Python JSONPath with Python Liquid and use Python Liquid's async protocol.

Extra filter context

findall() and finditer() take an optional filter_context argument, being a mapping of strings to arbitrary data that can be referenced from a filter expression.

Use # to query extra filter data, similar to how one might use @ or $.

Syntax

Python JSONPath's default syntax is an opinionated combination of JSONPath features from existing, popular implementations, and much of the IETF JSONPath draft. If you're already familiar with JSONPath syntax, skip to Notable differences.

TODO: tree analogy / target document TODO: use "node" terminology
TODO: mention JSON and Python equivalency

Root ($)

$ refers to the first node in the target document, be it an object or an array. Unless referencing the root node from inside a filter expression, $ is optional. The following two examples are equivalent.

$.categories.*.name
categories.*.name

Properties (.thing, [thing] or ['thing'])

Select nodes by property/key name using dot notation (.something) or bracket notation ([something]). If a target property/key contains reserved characters, it must use bracket notation and be enclosed in quotes (['thing']).

A dot in front of bracket notation is OK, but unnecessary. The following examples are equivalent.

$.categories[0].name
$.categories[0][name]
$.categories[0]['name']

Array indices (.0, [0] or [-1])

Select an item from an array by its index. Indices are zero-based and enclosed in brackets. If the index is negative, items are selected from the end of the array. Considering example data from the top of this page, the following examples are equivalent.

$.categories[0]
$.categories.0
$.categories[-1]

Wildcard (.* or [*])

Select all elements from an array or all values from an object using *. These two examples are equivalent.

$.categories[0].products.*
$.categories[0].products[*]

Slices ([0:-1] or [-1:0:-1])

Select a range of elements from an array using slice notation. The start index, stop index and step are all optional. These examples are equivalent.

$.categories[0:]
$.categories[0:-1:]
$.categories[0:-1:1]
$.categories[::]

Lists ([1, 2, 10:20])

Select multiple indices, slices or properties using list notation (sometimes known as a "union" or "segment", we use "union" to mean something else).

$..products.*.[title, price]

Recursive descent (..)

The .. selector visits every node beneath the current selection. If a property selector, using dot notation, follows .., the dot is optional. These two examples are equivalent.

$..title
$...title

Filters ([?(EXPRESSION)])

Filters allow you to remove nodes from a selection using a Boolean expression. Within a filter, @ refers to the current node and $ refers to the root node in the target document. @ and $ can be used to select nodes as part of the expression.

$..products.*[?(@.price < $.price_cap)]

Comparison operators include ==, !=, <, >, <= and >=. Plus <> as an alias for !=.

in and contains are membership operators. left in right is equivalent to right contains left.

&& and || are logical operators, and and or work too.

=~ matches the left value with a regular expression literal. Regular expressions use a similar syntax to that found in JavaScript, where the pattern to match is surrounded by slashes, optionally followed by flags.

$..products.*[?(@.description =~ /.*trainers/i)]

Union (|) and intersection (&)

TODO:

Notable differences

This is a list of things that you might find in other JSONPath implementation that we don't support (yet).

  • We don't support extension functions of the form selector.func().
  • We always return a list of matches from jsonpath.findall(), never a scalar value.
  • We do not support arithmetic in filter expression.

And this is a list of areas where we deviate from the IETF JSONPath draft.

  • We don't support extension functions of the form func(path, ..).
  • Whitespace is mostly insignificant unless inside quotes.
  • The root token (default $) is optional.
  • Paths starting with a dot (.) are OK. .thing is the same as $.thing, as is thing, $[thing] and $["thing"].
  • Nested filters are not supported.
  • When a filter is applied to an object (mapping) value, we do not silently apply that filter to the object's values. See the "Existence of non-singular queries" example in the IETF JSONPath draft.
  • | is a union operator, where matches from two or more JSONPaths are combined.
  • & is an intersection operator, where we output matches that exist in two paths.

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-0.1.0.tar.gz (924.7 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-0.1.0-py3-none-any.whl (25.1 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: python_jsonpath-0.1.0.tar.gz
  • Upload date:
  • Size: 924.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.10

File hashes

Hashes for python_jsonpath-0.1.0.tar.gz
Algorithm Hash digest
SHA256 fedcd99077a5a7393e86aba8f9e7dbd27324adbeb29cc9488c3d07f2eda131c3
MD5 0d7a12a646d0df818eec20fb3d7c2b0a
BLAKE2b-256 cf5f9e8930148e7a181139a58a5ef934a4adc1f4d14043e040818519d2aecb68

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for python_jsonpath-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 83a8478a36aa20f9a2839aed2857d9fd58192374a65eabd5fce115f174eb92b2
MD5 e7e3768ac3aad8e7bd15cb6c9b4d1419
BLAKE2b-256 78c6279c27bc3afb9e45c7d9c6c52e75c9a50c290aeba67f739a34005855e31c

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