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 details)

Uploaded Source

File details

Details for the file jpath-finder-1.0.1.tar.gz.

File metadata

  • Download URL: jpath-finder-1.0.1.tar.gz
  • Upload date:
  • Size: 10.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.7.1

File hashes

Hashes for jpath-finder-1.0.1.tar.gz
Algorithm Hash digest
SHA256 d79ee652da20e9b4c7c1b9a586d2f1454c6bdf742bde10a628aecd5f8b3e8d16
MD5 3852441c794d52e5c5e6886e368ae393
BLAKE2b-256 ce4c01a355a25680bdfeee640866459538b72107c6235285f413a314e457ebb7

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