Skip to main content

An implementation of Json Path for python.

Project description

An implementation of JsonPath for python with a good performance.

About

This library was created based on the <https://github.com/h2non/jsonpath-ng> library.

The following changes were made in order to improve the execution time and decrease memory usage:

  • Refactor in the JsonPath classes.

  • The datum_in_context class was removed.

  • Add new functionality like slice, sum, avg, and, or.

  • The Parser and Lexer method’s were changed to static methods.

  • Remove update and filter methods, this library just implement the find method.

  • Add new error classes.

Quick Start

To install, use pip:

$ pip install jpath-finder

Usage

Basic usage:

$ python
>>> from jpath_finder.jpath_parser import find

# find("jsonpath", json_data)
# Easy way to extract values through jsonpath.
>>> find("foo[*].baz", {"foo": [{"baz": 1}, {"baz": 2}]})
[1, 2]

# Also we can use an index '1' from a list or string.
>>> find("foo[1].baz", {"foo": [{"baz": 1}, {"baz": 2}]})
[2]

>>> find("foo[1][1:6]", {"foo": ["The example", "String example"]})
['tring']

>>> find("foo..baz", {"foo": [{"baz": 1}, {"baz": 2}]})
[1, 2]

# debug=True if you can log the error.
# find("jsonpath", json_data, debug=True)
>>> find("[2]", 123, debug=True)
JPathNodeError: Invalid Path [2] for 123
[]

# debug=False by default
# find("jsonpath", json_data)
>>> find("[2]", 123)
[]

# Note: if you have a custom_logger, it needs a debug method like "custom_logger.debug(message)".
# find("[2]", 123, logger=custom_logger, debug=True)

JSONPath Syntax

name

Syntax

Example

Root

“$”

  • $.objects

This

“@”

  • $.objects[?(@.name==”foo”)]

Descendant

“..”

  • $.objects..price

Union

“|”

  • $.objects_1 | $.objects_2

Fields

[0-9a-zA-Z]

  • $.objects.price.value

Index

[0-9]

  • $.objects[3].value

AllIndex

“[*]”

  • $.objects[*].cost

Slice

“[2:6:2]”

  • $.objects[2:20:3].cost

Sorted

sorted

  • $.objects.`sorted`

len

len

  • $.objects.`len`

sum

sum

  • $.objects.`sum`

avg

avg

  • $.objects.`avg`

filter

“[?(expr)]”

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

  • $.objects[?(some_field = “foobar”)]

  • $.objects[?(some_field != “foobar”)]

  • $.objects[?(some_field>5&other<2|other=3)]

arithmetic

“+” “*”

  • $.foo + “_” + $.bar

  • $.foo * 12

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

Contributors

This package is authored and maintained by:

  • William Alvarez <https://github.com/wapwallace>

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

jpath-finder-1.0.1.tar.gz (10.8 kB view hashes)

Uploaded Source

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page