-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjcat
executable file
·38 lines (33 loc) · 1.24 KB
/
jcat
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
37
38
#!/usr/bin/python
import json
import sys
from argparse import ArgumentParser
def combine(left, right):
if isinstance(left, list) and isinstance(right, list):
return left + right
elif isinstance(left, dict) and isinstance(right, dict):
left.update(right)
return left
else:
raise TypeError("Heterogenous JSON types, was (%s, %s)" % (type(left), type(right)))
if __name__ == '__main__':
parser = ArgumentParser()
parser.add_argument('files', nargs='+', help='Files containing JSON objects')
parser.add_argument('--wrap', action='store_true', help='Wrap JSON input in array')
parser.add_argument('--pretty', action='store_true', help='Pretty-prints JSON output')
arguments = parser.parse_args()
files = []
for filename in arguments.files:
with open(filename, 'r') as f:
try:
content = json.load(f)
files.append(content)
except Exception, e:
print "File '%s': %s" % (filename, e)
sys.exit()
if arguments.wrap:
result = files
else:
result = reduce(combine, files)
kwargs = {'sort_keys': True, 'indent': 4} if arguments.pretty else {}
print json.dumps(result, **kwargs)