Skip to main content

Python JSONPath Next-Generation

An implementation of JSONPath for Python that aims to be standards compliant, as defined in RFC 9535.

This packages merges both jsonpath-rw and jsonpath-rw-ext and provides several AST API enhancements, such as the ability to update or remove nodes in the tree.

About

This library provides a robust and significantly extended implementation of JSONPath for Python.

This library differs from other JSONPath implementations in that it is a full language implementation, meaning the JSONPath expressions are first class objects, easy to analyze, transform, parse, print, and extend.

Quick Start

To install, use pip:

$ pip install --upgrade jsonpath-ng

Usage

Basic examples:

>>> from jsonpath_ng import jsonpath, parse

# A robust parser, not just a regex. (Makes powerful extensions possible; see below)
>>> jsonpath_expr = parse('foo[*].baz')

# Extracting values is easy
>>> [match.value for match in jsonpath_expr.find({'foo': [{'baz': 1}, {'baz': 2}]})]
[1, 2]

# Matches remember where they came from
>>> [str(match.full_path) for match in jsonpath_expr.find({'foo': [{'baz': 1}, {'baz': 2}]})]
['foo.[0].baz', 'foo.[1].baz']

# Modifying values matching the path
>>> jsonpath_expr.update( {'foo': [{'baz': 1}, {'baz': 2}]}, 3)
{'foo': [{'baz': 3}, {'baz': 3}]}

# Modifying one of the values matching the path
>>> matches = jsonpath_expr.find({'foo': [{'baz': 1}, {'baz': 2}]})
>>> matches[0].full_path.update( {'foo': [{'baz': 1}, {'baz': 2}]}, 3)
{'foo': [{'baz': 3}, {'baz': 2}]}

# Removing all values matching a path
>>> jsonpath_expr.filter(lambda d: True, {'foo': [{'baz': 1}, {'baz': 2}]})
{'foo': [{}, {}]}

# Removing values containing particular data matching path
>>> jsonpath_expr.filter(lambda d: d == 2, {'foo': [{'baz': 1}, {'baz': 2}]})
{'foo': [{'baz': 1}, {}]}

# And this can be useful for automatically providing ids for bits of data that do not have them (currently a global switch)
>>> jsonpath.auto_id_field = 'id'
>>> [match.value for match in parse('foo[*].id').find({'foo': [{'id': 'bizzle'}, {'baz': 3}]})]
['foo.bizzle', 'foo.[1]']

# A handy extension: named operators like `parent`
>>> [match.value for match in parse('a.*.b.`parent`.c').find({'a': {'x': {'b': 1, 'c': 'number one'}, 'y': {'b': 2, 'c': 'number two'}}})]
['number one', 'number two']

# You can also build expressions directly quite easily
>>> from jsonpath_ng.jsonpath import Fields
>>> from jsonpath_ng.jsonpath import Slice

>>> parse('foo[*].baz') == Fields('foo').child(Slice()).child(Fields('baz'))
True

Using the extended parser:

>>> from jsonpath_ng.ext import parse
>>> jsonpath_expr = parse('foo[*].baz')

JSONPath Syntax

The JSONPath syntax supported by this library includes some additional features. In particular, some new operators such as where are available, and parentheses are used for grouping not for callbacks into Python, since with these changes the language is not trivially associative. Also, fields may be quoted whether or not they are contained in brackets.

Atomic expressions:

Syntax

Meaning

$

The root object

`this`

The “current” object.

`foo`

More generally, this syntax allows “named operators” to extend JSONPath is arbitrary ways

field

Specified field(s), described below

[ field ]

Same as field

[ idx ]

Array access, described below (this is always unambiguous with field access)

Jsonpath operators:

Syntax

Meaning

jsonpath1 . jsonpath2

All nodes matched by jsonpath2 starting at any node matching jsonpath1

jsonpath [ whatever ]

Same as jsonpath.whatever

jsonpath1 .. jsonpath2

All nodes matched by jsonpath2 that descend from any node matching jsonpath1

jsonpath1 where jsonpath2

Any nodes matching jsonpath1 with a child matching jsonpath2

jsonpath1 wherenot jsonpath2

Any nodes matching jsonpath1 with a child not matching jsonpath2

Field specifiers ( field ):

Syntax

Meaning

fieldname

the field fieldname (from the “current” object)

"fieldname"

same as above, for allowing special characters in the fieldname

'fieldname'

ditto

*

any field

field , field

either of the named fields (you can always build equivalent jsonpath using |)

Array specifiers ( idx ):

Syntax

Meaning

[n]

array index (may be comma-separated list)

[start?:end?:step?]

array slicing; start, end, and step are all optional

[*]

any array index

Programmatic JSONPath

If you are programming in Python and would like a more robust way to create JSONPath expressions that does not depend on a parser, it is very easy to do so directly, and here are some examples:

  • Root()

  • Slice(start=0, end=None, step=None)

  • Fields('foo', 'bar')

  • Index(42)

  • Child(Fields('foo'), Index(42))

  • Where(Slice(), Fields('subfield'))

  • Descendants(jsonpath, jsonpath)

Extras

  • Path data: The result of JsonPath.find provide detailed context and path data so it is easy to traverse to parent objects, print full paths to pieces of data, and generate automatic ids.

  • Automatic Ids: If you set jsonpath_ng.auto_id_field to a value other than None, then for any piece of data missing that field, it will be replaced by the JSONPath to it, giving automatic unique ids to any piece of data. These ids will take into account any ids already present as well.

  • Named operators: Instead of using @ to reference the current object, this library uses `this`. In general, any string contained in backquotes can be made to be a new operator, currently by extending the library.

Extensions

To use the extensions below you must import from jsonpath_ng.ext.

name

Example

len

  • $.objects.`len`

keys

  • $.objects.`keys`

str

  • $.field.`str()`

sub

  • $.field.`sub(/foo\\\\+(.*)/, \\\\1)`

  • $.field.`sub(/regex/, replacement)`

split

  • $.field.`split(+, 2, -1)`

  • $.field.`split(",", *, -1)`

  • $.field.`split(' ', -1, -1)`

  • $.field.`split(sep, segment, maxsplit)`

sorted

  • $.objects.`sorted`

  • $.objects[\\some_field]

  • $.objects[\\some_field,/other_field]

filter

  • $.objects[?(@.some_field > 5)]

  • $.objects[?(@.some_field = "foobar")]

  • $.objects[?(@.some_field =~ "foobar")]

  • $.objects[?@.cow > 5 && @.cat < 2]

  • $.objects[?@.cow > 5 || @.cat < 2]

  • $.objects[?!@.some_field]

Supported operators:

  • Equality: ==, =, !=

  • Comparison: >, >=, <, <=

  • Regex match: =~

  • Existence negation: !

Combine criteria with && (and) and || (or). && binds more tightly than ||, and parentheses group.

Properties can be compared to literal values or to singular queries, such as @.a == $.b.

arithmetic (-+*/)

  • $.foo + "_" + $.bar

  • $.foo * 12

  • $.objects[*].cow + $.objects[*].cat

About arithmetic and string

Operations are done with python operators and allows types that python allows, and return [] if the operation can’t be done due to incompatible types.

When operators are used, a jsonpath must be be fully defined otherwise jsonpath-rw-ext can’t known if the expression is a string or a jsonpath field, in this case it will choice string as type.

Example with data:

{
    'cow': 'foo',
    'fish': 'bar'
}
cow + fish returns cowfish
$.cow + $.fish returns foobar
$.cow + "_" + $.fish returns foo_bar
$.cow + "_" + fish returns foo_fish

About arithmetic and list

Arithmetic can be used against two lists if they have the same size.

Example with data:

{'objects': [
    {'cow': 2, 'cat': 3},
    {'cow': 4, 'cat': 6}
]}
$.objects[\*].cow + $.objects[\*].cat returns [6, 9]

Other examples

Loading json data from file

import json
d = json.loads('{"foo": [{"baz": 1}, {"baz": 2}]}')
# or
with open('myfile.json') as f:
    d = json.load(f)

Contributors

This package is authored and maintained by:

with the help of patches submitted by these contributors.

Metadata

Release files for jsonpath-ng 1.13.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for jsonpath-ng 1.13.0
File Size Uploaded Provenance
jsonpath_ng-1.13.0.tar.gz 116.4 kB With provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for jsonpath-ng 1.13.0
File Interpreter ABI Platform
jsonpath_ng-1.13.0-py3-none-any.whl Python 3 none any Details

Total release size: 200.3 kB

Release files / jsonpath_ng-1.13.0.tar.gz

Download URL jsonpath_ng-1.13.0.tar.gz
Size 116.4 kB
Tags Source
SHA-256 checksum
How to use checksums
da54eff64eef303b90a8491f50c607e6beb2eb4a6e66828edab948766d40c531
BLAKE2b-256 checksum
How to use checksums
4aa44db8bd69f41fd56bdc0a709137c10a410e5cc148b3760ea206a510128ae6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 23, 2026.

Transparency log

Release files / jsonpath_ng-1.13.0-py3-none-any.whl

Download URL jsonpath_ng-1.13.0-py3-none-any.whl
Size 84.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
f58e7f88cdd2eb04a402c0d542e7d6db71909e521f4c23c62f4d2ee73266361e
BLAKE2b-256 checksum
How to use checksums
5c0330db4cd2e778aee7feb29b14ff9c831735e5e1939600dff2bacba38107b1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 23, 2026.

Transparency log

Security for jsonpath-ng 1.13.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

All 2 files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for jsonpath_ng-1.13.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 23, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page