-
Notifications
You must be signed in to change notification settings - Fork 0
/
merge_jsons.py
44 lines (33 loc) · 896 Bytes
/
merge_jsons.py
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
39
40
41
42
43
44
import argparse
import sys
import json
def merge_dicts(a, b):
result = a
for k, v in b.items():
result[k] = v
return result
def main() -> None:
parser = argparse.ArgumentParser("Merge jsons.")
parser.add_argument(
"output_json_path",
metavar="OUTPUT_JSON_PATH",
type=str,
help="path to output json"
)
parser.add_argument(
"input_json_paths",
metavar="INPUT_JSON_PATHs",
type=str,
nargs="+",
help="path to input jsons"
)
args = parser.parse_args()
result = {}
for path in args.input_json_paths:
with open(path, "r") as handle:
tmp = json.load(handle)
result = merge_dicts(result, tmp)
with open(args.output_json_path, "w") as handle:
json.dump(result, handle)
if __name__ == "__main__":
main()