-
Notifications
You must be signed in to change notification settings - Fork 0
/
jsonx
executable file
·38 lines (29 loc) · 883 Bytes
/
jsonx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#!/usr/bin/env python
import os
import sys
import argparse
import dpath.util
import json
def parse_args():
p = argparse.ArgumentParser()
p.add_argument('--keys', '-k', action='store_true')
p.add_argument('--values', '-v', action='store_true')
p.add_argument('--pretty', '-p', action='store_true')
p.add_argument('paths', nargs='*')
return p.parse_args()
def main():
args = parse_args()
cfg = json.load(sys.stdin)
for pathexp in args.paths:
if args.pretty:
print json.dumps(dpath.util.search(cfg, pathexp), indent=4)
else:
for path,val in dpath.util.search(cfg, pathexp, True):
if args.keys:
print path
elif args.values:
print val
else:
print path, val
if __name__ == '__main__':
main()