Skip to main content

JSONPath implementation for Python

Project description

jsonpath2

Build Status

This repository contains an implementation of JSONPath (XPath for JSON) for the Python programming language.

API

Path class

The jsonpath2.Path.Path class represents a JSONPath.

>>> s = '{"hello":"Hello, world!"}'
'{"hello":"Hello, world!"}'
>>> import json
>>> d = json.loads(s)
{'hello':'Hello, world!'}
>>> from jsonpath2.path import Path
>>> p = Path.parse_str('$["hello"]')
<jsonpath2.path.Path object>
>>> list(map(lambda match_data: match_data.current_value, p.match(d)))
['Hello, world!']
>>> list(map(lambda match_data: match_data.node.tojsonpath(), p.match(d)))
['$["hello"]']

This class is constructed with respect to the given instance of the jsonpath2.Path.RootNode class (viz., the root_node property).

parse_str(strdata) class method

Parse the given string and return a new instance of this class.

parse_file(fileName, encoding='ascii') class method

Parse the contents of the given file and return a new instance of this class.

match(root_value) instance method

Match the given JSON data structure against this instance. For each match, yield an instance of the jsonpath2.Node.MatchData class.

__eq__(other) instance method

Tests if two instances are equal.

__str__() instance method

Returns the string representation of this instance.

root_node property

The root node of the abstract syntax tree for this instance.

Node abstract class

The jsonpath2.Node.Node class represents the abstract syntax tree for a JSONPath.

__eq__(other) instance method

Tests if two instances are equal.

__jsonpath__() instance method

Yields the lexer tokens for the string representation of this instance.

match(root_value, current_value) instance method

Match the given root and current JSON data structures against this instance. For each match, yield an instance of the jsonpath2.Node.MatchData class.

tojsonpath() instance method

Returns the string representation of this instance.

MatchData class

The jsonpath2.Node.MatchData class represents the JSON value and context for a JSONPath match.

This class is constructed with respect to a root JSON value, a current JSON value, and an abstract syntax tree node.

__eq__(other) instance method

Tests if two instances are equal.

root_value property

The root JSON value.

current_value property

The current JSON value (i.e., the matching JSON value).

node property

The abstract syntax tree node.

Syntax

XPath JSONPath Description
/ $ the root JSON value
. @ the current JSON value
/ . or [] child operator
// .. recursive descent (depth-first search)
* * wildcard (all elements of a JSON array; all values of a JSON object; otherwise none)
[] [] subscript operator
| [,] union operator (for two or more subscript operators)
n/a [start:end:step] slice operator (subset of elements of a JSON array)
[] ?() filter expression (for use with subscript operator)
JSONPath Filter Expression Description
$ or @ nested JSONPath (returns true if any match exists; otherwise, returns false)
=, !=, >, >=, <, <= binary operator, where left-hand operand is a nested JSONPath and right-right operand is a JSON value (returns true if any match exists; otherwise, returns false)
and, or, not Boolean operator, where operands are JSONPath filter expressions
( ... ) parentheses

Grammar and parser

The ANTLR v4 grammar for JSONPath is available at jsonpath2/parser/JSONPath.g4.

Installing ANTLR v4

Adapted from https://github.com/antlr/antlr4/blob/master/doc/getting-started.md.

cd /usr/local/lib
curl -O http://www.antlr.org/download/antlr-4.7.1-complete.jar

export CLASSPATH=".:/usr/local/lib/antlr-4.7.1-complete.jar:$CLASSPATH"

alias antlr4='java -Xmx500M -cp "/usr/local/lib/antlr-4.7.1-complete.jar:$CLASSPATH" org.antlr.v4.Tool'
alias grun='java org.antlr.v4.gui.TestRig'

Building the parser for the grammar

Adapted from https://github.com/antlr/antlr4/blob/master/doc/python-target.md.

antlr4 -Dlanguage=Python3 -o . -lib . jsonpath2/parser/JSONPath.g4

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

jsonpath2-0.2.0.tar.gz (35.5 kB view details)

Uploaded Source

Built Distribution

jsonpath2-0.2.0-py3-none-any.whl (31.4 kB view details)

Uploaded Python 3

File details

Details for the file jsonpath2-0.2.0.tar.gz.

File metadata

  • Download URL: jsonpath2-0.2.0.tar.gz
  • Upload date:
  • Size: 35.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.6.3

File hashes

Hashes for jsonpath2-0.2.0.tar.gz
Algorithm Hash digest
SHA256 362ad929b31a07f053139bebae6695e2595121ca5496050a269446904e34bd23
MD5 67b4709b06b65eefacbad73efb0ca2cf
BLAKE2b-256 b5b81e4a4133d184ea12534338f1a6a120a917689d4c431b088df4a80c953777

See more details on using hashes here.

File details

Details for the file jsonpath2-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: jsonpath2-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 31.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.6.3

File hashes

Hashes for jsonpath2-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 447efbf705fbb48c0076f36c6950b3d8109ce0c174cad0e22884475eb16bdb9b
MD5 ee2277866b5feaf77d6192e941e24da3
BLAKE2b-256 383ec2767a158fcb798a6a978dc743295831ac2355bf81e2284fc9a48f589ae1

See more details on using hashes here.

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