Skip to content

Commit be219c3

Browse files
authored
feat: command line interface (jg-rp#26)
* feat: command line interface
1 parent a1af7e2 commit be219c3

15 files changed

Lines changed: 1222 additions & 31 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Python JSONPath Change Log
22

3+
## Version 0.9.0 (unreleased)
4+
5+
**Features**
6+
7+
- Added a command line interface, exposing JSONPath, JSON Pointer and JSON Patch features.
8+
39
## Version 0.8.1
410

511
**Fixes**

‎docs/cli.md‎

Lines changed: 306 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,306 @@
1+
# Command Line Interface
2+
3+
**_New in version 0.9.0_**
4+
5+
Python JSONPath includes a script called `json`, exposing [JSONPath](quickstart.md#findallpath-data), [JSON Pointer](quickstart.md#pointerresolvepointer-data) and [JSON Patch](quickstart.md#patchapplypatch-data) features on the command line. Use the `--version` argument to check the current version of Python JSONPath, and the `--help` argument to display command information.
6+
7+
8+
```console
9+
$ json --version
10+
python-jsonpath, version 0.9.0
11+
```
12+
13+
```console
14+
$ json --help
15+
usage: json [-h] [--debug] [--pretty] [-v] [--no-unicode-escape] COMMAND ...
16+
17+
JSONPath, JSON Pointer and JSON Patch utilities.
18+
19+
positional arguments:
20+
COMMAND
21+
path Find objects in a JSON document given a JSONPath.
22+
pointer Resolve a JSON Pointer against a JSON document.
23+
patch Apply a JSON Patch to a JSON document.
24+
25+
optional arguments:
26+
-h, --help show this help message and exit
27+
--debug Show stack traces. (default: False)
28+
--pretty Add indents and newlines to output JSON. (default: False)
29+
-v, --version Show the version and exit.
30+
--no-unicode-escape Disable decoding of UTF-16 escape sequence within paths and pointers. (default:
31+
False)
32+
33+
Use [json COMMAND --help] for command specific help.
34+
35+
Usage Examples:
36+
Find objects in source.json matching a JSONPath, write them to result.json.
37+
$ json path -q "$.foo['bar'][[email protected] > 1]" -f source.json -o result.json
38+
39+
Resolve a JSON Pointer against source.json, pretty print the result to stdout.
40+
$ json --pretty pointer -p "/foo/bar/0" -f source.json
41+
42+
Apply JSON Patch patch.json to JSON from stdin, output to result.json.
43+
$ cat source.json | json patch /path/to/patch.json -o result.json
44+
```
45+
46+
Use `json COMMAND --help` for command specific help.
47+
48+
```console
49+
$ json path --help
50+
usage: json path [-h] (-q QUERY | -r PATH_FILE) [-f FILE] [-o OUTPUT]
51+
52+
Find objects in a JSON document given a JSONPath.
53+
54+
optional arguments:
55+
-h, --help show this help message and exit
56+
-q QUERY, --query QUERY
57+
JSONPath query string.
58+
-r PATH_FILE, --path-file PATH_FILE
59+
Text file containing a JSONPath query.
60+
-f FILE, --file FILE File to read the target JSON document from. Defaults to reading from the
61+
standard input stream.
62+
-o OUTPUT, --output OUTPUT
63+
File to write resulting objects to, as a JSON array. Defaults to the standard
64+
output stream.
65+
```
66+
67+
## Global Options
68+
69+
These arguments apply to any subcommand and must be listed before the command.
70+
71+
### `--debug`
72+
73+
Enable debugging. Display full stack traces, if available, when errors occur. Without the `--debug` option, the following example shows a short "json path syntax error" message.
74+
75+
```console
76+
$ json path -q "$.1" -f /tmp/source.json
77+
json path syntax error: unexpected token '1', line 1, column 2
78+
```
79+
80+
With the `--debug` option, we get the stack trace triggered by `JSONPathSyntaxError`.
81+
82+
```console
83+
$ json --debug path -q "$.1" -f /tmp/source.json
84+
Traceback (most recent call last):
85+
File "/home/james/.local/share/virtualenvs/jsonpath_cli-8Tb3e-ir/bin/json", line 8, in <module>
86+
sys.exit(main())
87+
File "/home/james/.local/share/virtualenvs/jsonpath_cli-8Tb3e-ir/lib/python3.9/site-packages/jsonpath/cli.py", line 338, in main
88+
args.func(args)
89+
File "/home/james/.local/share/virtualenvs/jsonpath_cli-8Tb3e-ir/lib/python3.9/site-packages/jsonpath/cli.py", line 234, in handle_path_command
90+
path = jsonpath.compile(args.query or args.path_file.read())
91+
File "/home/james/.local/share/virtualenvs/jsonpath_cli-8Tb3e-ir/lib/python3.9/site-packages/jsonpath/env.py", line 148, in compile
92+
_path: Union[JSONPath, CompoundJSONPath] = JSONPath(
93+
File "/home/james/.local/share/virtualenvs/jsonpath_cli-8Tb3e-ir/lib/python3.9/site-packages/jsonpath/path.py", line 49, in __init__
94+
self.selectors = tuple(selectors)
95+
File "/home/james/.local/share/virtualenvs/jsonpath_cli-8Tb3e-ir/lib/python3.9/site-packages/jsonpath/parse.py", line 256, in parse
96+
raise JSONPathSyntaxError(
97+
jsonpath.exceptions.JSONPathSyntaxError: unexpected token '1', line 1, column 2
98+
```
99+
100+
### `--pretty`
101+
102+
Enable pretty formatting when outputting JSON. Adds newlines and indentation to output specified with the `-o` or `--output` option. Without the `--pretty` option, the following example output is on one line.
103+
104+
```console
105+
$ json pointer -p "/categories/1/products/0" -f /tmp/source.json
106+
{"title": "Cap", "description": "Baseball cap", "price": 15.0}
107+
```
108+
109+
With the `--pretty` option, we get nicely formatted JSON output.
110+
111+
```console
112+
$ json --pretty pointer -p "/categories/1/products/0" -f /tmp/source.json
113+
{
114+
"title": "Cap",
115+
"description": "Baseball cap",
116+
"price": 15.0
117+
}
118+
```
119+
120+
### `--no-unicode-escape`
121+
122+
Disable decoding of UTF-16 escape sequences, including surrogate paris. This can improve performance if you know your paths and pointers don't contain UTF-16 escape sequences.
123+
124+
```console
125+
$ json --no-unicode-escape path -q "$.price_cap" -f /tmp/source.json
126+
```
127+
128+
## Commands
129+
130+
One of the subcommands `path`, `pointer` or `patch` must be specified, depending on whether you want to search a JSON document with a JSONPath, resolve a JSON Pointer against a JSON document or apply a JSON Patch to a JSON Document.
131+
132+
### `path`
133+
134+
Find objects in a JSON document given a JSONPath. One of `-q`/`--query` or `-r`/`--path-file` must be given. `-q` being a JSONPath given on the command line as a string, `-r` being the path to a file containing a JSONPath.
135+
136+
```
137+
json path [-h] (-q QUERY | -r PATH_FILE) [-f FILE] [-o OUTPUT]
138+
```
139+
140+
#### `-q` / `--query`
141+
142+
The JSONPath as a string.
143+
144+
```console
145+
$ json path -q "$.price_cap" -f /tmp/source.json
146+
```
147+
148+
```console
149+
$ json path --query "$.price_cap" -f /tmp/source.json
150+
```
151+
152+
#### `-r` / `--path-file`
153+
154+
The path to a file containing a JSONPath.
155+
156+
```console
157+
$ json path -r /tmp/path.txt -f /tmp/source.json
158+
```
159+
160+
```console
161+
$ json path --path-file /tmp/path.txt -f /tmp/source.json
162+
```
163+
164+
#### `-f` / `--file`
165+
166+
The path to a file containing the target JSON document. If omitted or a hyphen (`-`), the target JSON document will be read from the standard input stream.
167+
168+
```console
169+
$ json path -q "$.price_cap" -f /tmp/source.json
170+
```
171+
172+
```console
173+
$ json path -q "$.price_cap" --file /tmp/source.json
174+
```
175+
176+
#### `-o` / `--output`
177+
178+
The path to a file to write resulting objects to, as a JSON array. If omitted or a hyphen (`-`) is given, results will be written to the standard output stream.
179+
180+
```console
181+
$ json path -q "$.price_cap" -f /tmp/source.json -o result.json
182+
```
183+
184+
```console
185+
$ json path -q "$.price_cap" -f /tmp/source.json --output result.json
186+
```
187+
188+
### `pointer`
189+
190+
Resolve a JSON Pointer against a JSON document. One of `-p`/`--pointer` or `-r`/`--pointer-file` must be given. `-p` being a JSON Pointer given on the command line as a string, `-r` being the path to a file containing a JSON Pointer.
191+
192+
```
193+
json pointer [-h] (-p POINTER | -r POINTER_FILE) [-f FILE] [-o OUTPUT] [-u]
194+
```
195+
196+
#### `-p` / `--pointer`
197+
198+
An RFC 6901 formatted JSON Pointer string.
199+
200+
```console
201+
$ json pointer -p "/categories/0/name" -f /tmp/source.json
202+
```
203+
204+
```console
205+
$ json pointer --pointer "/categories/0/name" -f /tmp/source.json
206+
```
207+
208+
#### `-r` / `--pointer-file`
209+
210+
The path to a file containing a JSON Pointer.
211+
212+
```console
213+
$ json pointer -r /tmp/pointer.txt -f /tmp/source.json
214+
```
215+
216+
```console
217+
$ json pointer --pointer-file /tmp/pointer.txt -f /tmp/source.json
218+
```
219+
220+
#### `-f` / `--file`
221+
222+
The path to a file containing the target JSON document. If omitted or a hyphen (`-`), the target JSON document will be read from the standard input stream.
223+
224+
```console
225+
$ json pointer -p "/categories/0/name" -f /tmp/source.json
226+
```
227+
228+
```console
229+
$ json pointer -p "/categories/0/name" --file /tmp/source.json
230+
```
231+
232+
#### `-o` / `--output`
233+
234+
The path to a file to write the resulting object to. If omitted or a hyphen (`-`) is given, results will be written to the standard output stream.
235+
236+
```console
237+
$ json pointer -p "/categories/0/name" -f /tmp/source.json -o result.json
238+
```
239+
```console
240+
$ json pointer -p "/categories/0/name" -f /tmp/source.json --output result.json
241+
```
242+
243+
#### `-u` / `--uri-decode`
244+
245+
Enable URI decoding of the JSON Pointer. In this example, we would look for a property called "hello world" in the root of the target document.
246+
247+
```console
248+
$ json pointer -p "/hello%20world" -f /tmp/source.json -u
249+
```
250+
251+
```console
252+
$ json pointer -p "/hello%20world" -f /tmp/source.json --uri-decode
253+
```
254+
255+
### `patch`
256+
257+
Apply a JSON Patch to a JSON document. Unlike `path` and `pointer` commands, a patch can't be given as a string argument. `PATCH` is a positional argument that should be a file path to a JSON Patch document or a hyphen (`-`), which means the patch document will be read from the standard input stream.
258+
259+
```
260+
json patch [-h] [-f FILE] [-o OUTPUT] [-u] PATCH
261+
```
262+
263+
These examples read the patch from `patch.json` and the document to modify from `target.json`
264+
265+
```console
266+
$ json patch /tmp/patch.json -f /tmp/target.json
267+
```
268+
269+
```console
270+
$ cat /tmp/patch.json | json patch - -f /tmp/target.json
271+
```
272+
273+
#### `-f` / `--file`
274+
275+
The path to a file containing the target JSON document. If omitted or a hyphen (`-`), the target JSON document will be read from the standard input stream.
276+
277+
```console
278+
$ json patch /tmp/patch.json -f /tmp/target.json
279+
```
280+
281+
```console
282+
$ json patch /tmp/patch.json --file /tmp/target.json
283+
```
284+
285+
#### `-o` / `--output`
286+
287+
The path to a file to write the resulting object to. If omitted or a hyphen (`-`) is given, results will be written to the standard output stream.
288+
289+
```console
290+
$ json patch /tmp/patch.json -f /tmp/target.json -o result.json
291+
```
292+
```console
293+
$ json patch /tmp/patch.json -f /tmp/target.json --output result.json
294+
```
295+
296+
#### `-u` / `--uri-decode`
297+
298+
Enable URI decoding of JSON Pointers in the patch document.
299+
300+
```console
301+
$ json patch /tmp/patch.json -f /tmp/target.json -u
302+
```
303+
304+
```console
305+
$ json patch /tmp/patch.json -f /tmp/target.json --uri-decode
306+
```

‎docs/index.md‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,12 @@ Or [Pipenv](https://pipenv.pypa.io/en/latest/):
2020
pipenv install python-jsonpath
2121
```
2222

23+
Or [pipx](https://pypa.github.io/pipx/)
24+
25+
```console
26+
pipx install python-jsonpath
27+
```
28+
2329
Or from [conda-forge](https://anaconda.org/conda-forge/python-jsonpath):
2430

2531
```console

‎jsonpath/__about__.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
# SPDX-FileCopyrightText: 2023-present James Prior <[email protected]>
22
#
33
# SPDX-License-Identifier: MIT
4-
__version__ = "0.8.1"
4+
__version__ = "0.9.0"

‎jsonpath/__main__.py‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
"""CLI entry point."""
2+
from jsonpath.cli import main
3+
4+
main()

0 commit comments

Comments
 (0)