File tree Expand file tree Collapse file tree 2 files changed +52
-0
lines changed Expand file tree Collapse file tree 2 files changed +52
-0
lines changed Original file line number Diff line number Diff line change 1+ #!/usr/bin/python
2+ # encoding: utf-8
3+ # Copyright © 2012 Felix Richter <wtfpl@syntax-fehler.de>
4+ # This work is free. You can redistribute it and/or modify it under the
5+ # terms of the Do What The Fuck You Want To Public License, Version 2,
6+ # as published by Sam Hocevar. See the COPYING file for more details.
7+
8+ from jsonpath_rw import parse
9+ import json
10+ import sys
11+ import glob
12+ if len (sys .argv ) < 2 :
13+ print ("""usage: jsonpath.py expression [files]
14+
15+ The expression is JSONPath and can be:
16+
17+ atomics:
18+ $ - root object
19+ `this` - current object
20+
21+ operators:
22+ path1.path2 - same as xpath /
23+ path1|path2 - union
24+ path1..path2 - somewhere in between
25+
26+ fiels:
27+ fieldname - field with name
28+ * - any field
29+ [_start_?:_end_?] - array slice
30+ [*] - any array index
31+ """ )
32+ sys .exit (1 )
33+
34+ expr = parse (sys .argv [1 ])
35+
36+ def find_matches_for_file (f ):
37+ return [unicode (match .value ) for match in expr .find (json .load (f ))]
38+
39+ def print_matches (matches ):
40+ print (u"\n " .join (matches ).encode ("utf-8" ))
41+
42+ if len (sys .argv ) < 3 :
43+ # stdin mode
44+ print_matches (find_matches_for_file (sys .stdin ))
45+ else :
46+ # file paths mode
47+ for pattern in sys .argv [2 :]:
48+ for filename in glob .glob (pattern ):
49+ with open (filename ) as f :
50+ print_matches (find_matches_for_file (f ))
51+
Original file line number Diff line number Diff line change 1414 license = 'Apache 2.0' ,
1515 long_description = io .open ('README.rst' , encoding = 'utf-8' ).read (),
1616 packages = ['jsonpath_rw' ],
17+ scripts = ['jsonpath_rw/bin/jsonpath.py' ],
1718 test_suite = 'tests' ,
1819 install_requires = [ 'ply' , 'decorator' , 'six' ],
1920 classifiers = [
You can’t perform that action at this time.
0 commit comments