|
1 | | -# Python JSONPath2 |
| 1 | +# jsonpath2 |
2 | 2 | [](https://travis-ci.org/pacifica/python-jsonpath2) |
3 | | -A JSONPath implementation for Python (but better than jsonpath). |
| 3 | + |
| 4 | +This repository contains an implementation of [JSONPath](http://goessner.net/articles/JsonPath/) ([XPath](https://www.w3.org/TR/xpath/all/) for [JSON](https://www.json.org/)) for the Python programming language. |
| 5 | + |
| 6 | +## API |
| 7 | + |
| 8 | +### `Path` class |
| 9 | + |
| 10 | +The `jsonpath2.Path.Path` class represents a JSONPath. |
| 11 | + |
| 12 | +```python |
| 13 | +>>> s = '{"hello":"Hello, world!"}' |
| 14 | +'{"hello":"Hello, world!"}' |
| 15 | +>>> import json |
| 16 | +>>> d = json.loads(s) |
| 17 | +{'hello':'Hello, world!'} |
| 18 | +>>> from jsonpath2.path import Path |
| 19 | +>>> p = Path.parse_str('$["hello"]') |
| 20 | +<jsonpath2.path.Path object> |
| 21 | +>>> list(map(lambda match_data: match_data.current_value, p.match(d))) |
| 22 | +['Hello, world!'] |
| 23 | +>>> list(map(lambda match_data: match_data.node.tojsonpath(), p.match(d))) |
| 24 | +['$["hello"]'] |
| 25 | +``` |
| 26 | + |
| 27 | +This class is constructed with respect to the given instance of the `jsonpath2.Path.RootNode` class (viz., the `root_node` property). |
| 28 | + |
| 29 | +#### `parse_str(strdata)` class method |
| 30 | + |
| 31 | +Parse the given string and return a new instance of this class. |
| 32 | + |
| 33 | +#### `parse_file(fileName, encoding='ascii')` class method |
| 34 | + |
| 35 | +Parse the contents of the given file and return a new instance of this class. |
| 36 | + |
| 37 | +#### `match(root_value)` instance method |
| 38 | + |
| 39 | +Match the given JSON data structure against this instance. |
| 40 | +For each match, yield an instance of the `jsonpath2.Node.MatchData` class. |
| 41 | + |
| 42 | +#### `__eq__(other)` instance method |
| 43 | + |
| 44 | +Tests if two instances are equal. |
| 45 | + |
| 46 | +#### `__str__()` instance method |
| 47 | + |
| 48 | +Returns the string representation of this instance. |
| 49 | + |
| 50 | +#### `root_node` property |
| 51 | + |
| 52 | +The root node of the abstract syntax tree for this instance. |
| 53 | + |
| 54 | +### `Node` abstract class |
| 55 | + |
| 56 | +The `jsonpath2.Node.Node` class represents the abstract syntax tree for a JSONPath. |
| 57 | + |
| 58 | +#### `__eq__(other)` instance method |
| 59 | + |
| 60 | +Tests if two instances are equal. |
| 61 | + |
| 62 | +#### `__jsonpath__()` instance method |
| 63 | + |
| 64 | +Yields the lexer tokens for the string representation of this instance. |
| 65 | + |
| 66 | +#### `match(root_value, current_value)` instance method |
| 67 | + |
| 68 | +Match the given root and current JSON data structures against this instance. |
| 69 | +For each match, yield an instance of the `jsonpath2.Node.MatchData` class. |
| 70 | + |
| 71 | +#### `tojsonpath()` instance method |
| 72 | + |
| 73 | +Returns the string representation of this instance. |
| 74 | + |
| 75 | +### `MatchData` class |
| 76 | + |
| 77 | +The `jsonpath2.Node.MatchData` class represents the JSON value and context for a JSONPath match. |
| 78 | + |
| 79 | +This class is constructed with respect to a root JSON value, a current JSON value, and an abstract syntax tree node. |
| 80 | + |
| 81 | +#### `__eq__(other)` instance method |
| 82 | + |
| 83 | +Tests if two instances are equal. |
| 84 | + |
| 85 | +#### `root_value` property |
| 86 | + |
| 87 | +The root JSON value. |
| 88 | + |
| 89 | +#### `current_value` property |
| 90 | + |
| 91 | +The current JSON value (i.e., the matching JSON value). |
| 92 | + |
| 93 | +#### `node` property |
| 94 | + |
| 95 | +The abstract syntax tree node. |
| 96 | + |
| 97 | +## Syntax |
| 98 | + |
| 99 | +| XPath | JSONPath | Description | |
| 100 | +| - | - | - | |
| 101 | +| `/` | `$` | the root JSON value | |
| 102 | +| `.` | `@` | the current JSON value | |
| 103 | +| `/` | `.` or `[]` | child operator | |
| 104 | +| `//` | `..` | recursive descent (depth-first search) | |
| 105 | +| `*` | `*` | wildcard (all elements of a JSON array; all values of a JSON object; otherwise none) | |
| 106 | +| `[]` | `[]` | subscript operator | |
| 107 | +| <code>|</code> | `[,]` | union operator (for two or more subscript operators) | |
| 108 | +| n/a | `[start:end:step]` | slice operator (subset of elements of a JSON array) | |
| 109 | +| `[]` | `?()` | filter expression (for use with subscript operator) | |
| 110 | + |
| 111 | +| JSONPath Filter Expression | Description | |
| 112 | +| - | - | |
| 113 | +| `$` or `@` | nested JSONPath (returns `true` if any match exists; otherwise, returns `false`) | |
| 114 | +| `=`, `!=`, `>`, `>=`, `<`, `<=` | 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`) | |
| 115 | +| `and`, `or`, `not` | Boolean operator, where operands are JSONPath filter expressions | |
| 116 | +| `(` ... `)` | parentheses | |
| 117 | + |
| 118 | +## Grammar and parser |
| 119 | + |
| 120 | +The [ANTLR v4](https://github.com/antlr/antlr4) grammar for JSONPath is available at `jsonpath2/parser/JSONPath.g4`. |
| 121 | + |
| 122 | +### Installing ANTLR v4 |
| 123 | + |
| 124 | +Adapted from https://github.com/antlr/antlr4/blob/master/doc/getting-started.md. |
| 125 | + |
| 126 | +```bash |
| 127 | +cd /usr/local/lib |
| 128 | +curl -O http://www.antlr.org/download/antlr-4.7.1-complete.jar |
| 129 | + |
| 130 | +export CLASSPATH=".:/usr/local/lib/antlr-4.7.1-complete.jar:$CLASSPATH" |
| 131 | + |
| 132 | +alias antlr4='java -Xmx500M -cp "/usr/local/lib/antlr-4.7.1-complete.jar:$CLASSPATH" org.antlr.v4.Tool' |
| 133 | +alias grun='java org.antlr.v4.gui.TestRig' |
| 134 | +``` |
| 135 | + |
| 136 | +### Building the parser for the grammar |
| 137 | + |
| 138 | +Adapted from https://github.com/antlr/antlr4/blob/master/doc/python-target.md. |
| 139 | + |
| 140 | +```bash |
| 141 | +antlr4 -Dlanguage=Python3 -o . -lib . jsonpath2/parser/JSONPath.g4 |
| 142 | +``` |
0 commit comments