Codebase list python-json-pointer / b607526
add "jsonpointer" commandline utility Stefan Kögl 10 years ago
4 changed file(s) with 85 addition(s) and 0 deletion(s). Raw diff Collapse all Expand all
0 #!/usr/bin/env python
1 # -*- coding: utf-8 -*-
2
3 from __future__ import print_function
4
5 import sys
6 import os.path
7 import json
8 import jsonpointer
9 import argparse
10
11
12 parser = argparse.ArgumentParser(
13 description='Resolve a JSON pointer on JSON files')
14 parser.add_argument('POINTER', type=argparse.FileType('r'),
15 help='File containing a JSON pointer expression')
16 parser.add_argument('FILE', type=argparse.FileType('r'), nargs='+',
17 help='Files for which the pointer should be resolved')
18 parser.add_argument('--indent', type=int, default=None,
19 help='Indent output by n spaces')
20 parser.add_argument('-v', '--version', action='version',
21 version='%(prog)s ' + jsonpointer.__version__)
22
23
24 def main():
25 try:
26 resolve_files()
27 except KeyboardInterrupt:
28 sys.exit(1)
29
30
31 def resolve_files():
32 """ Resolve a JSON pointer on JSON files """
33 args = parser.parse_args()
34 ptr = json.load(args.POINTER)
35 for f in args.FILE:
36 doc = json.load(f)
37 try:
38 result = jsonpointer.resolve_pointer(doc, ptr)
39 print(json.dumps(result, indent=args.indent))
40 except jsonpointer.JsonPointerException as e:
41 print('Could not resolve pointer: %s' % str(e), file=sys.stderr)
42
43
44 if __name__ == "__main__":
45 main()
0 The ``jsonpointer`` commandline utility
1 =======================================
2
3 The JSON pointer package also installs a ``jsonpointer`` commandline utility
4 that can be used to resolve a JSON pointers on JSON files.
5
6 The program has the following usage ::
7
8 usage: jsonpointer [-h] [--indent INDENT] [-v] POINTER FILE [FILE ...]
9
10 Resolve a JSON pointer on JSON files
11
12 positional arguments:
13 POINTER File containing a JSON pointer expression
14 FILE Files for which the pointer should be resolved
15
16 optional arguments:
17 -h, --help show this help message and exit
18 --indent INDENT Indent output by n spaces
19 -v, --version show program's version number and exit
20
21
22 The following shows example usage ::
23
24 $ cat a.json
25 { "a": [1, 2, 3] }
26 $ cat b.json
27 { "a": {"b": [1, 3, 4]}, "b": 1 }
28 $ cat ptr.json
29 "/a"
30 $ jsonpointer ptr.json a.json b.json
31 [1, 2, 3]
32 {"b": [1, 3, 4]}
1616
1717 tutorial
1818 mod-jsonpointer
19 commandline
1920 RFC 6901 <http://tools.ietf.org/html/rfc6901>
2021
2122
3232 license=LICENSE,
3333 url=WEBSITE,
3434 py_modules=MODULES,
35 scripts=['bin/jsonpointer'],
36 entry_poimts = {
37 'console_scripts': [
38 'jsonpointer = jsonpointer:main',
39 ]},
3540 )