-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflat_json_search.py
87 lines (73 loc) · 2.12 KB
/
flat_json_search.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import argparse
import json
import sys
import os
parser = argparse.ArgumentParser()
parser.add_argument('-k', '--key',
action="store", dest="key",
help="Key string", required=True)
parser.add_argument('-v', '--value',
action="store", dest="value",
help="Value string")
parser.add_argument("-d", "--dir", dest="path",
help="path to search for jsons", default=os.getcwd())
args = parser.parse_args()
def flatten_json(json_str):
out = {}
def flatten(x, name=''):
if type(x) is dict:
for a in x:
flatten(x[a], name + a + '_')
elif type(x) is list:
i = 0
for a in x:
flatten(a, name + str(i) + '_')
i += 1
else:
out[name[:-1]] = x
flatten(json_str)
return out
def check_key(key_check,dict):
result = {}
for key, value in dict.items():
parts = key.split("_")
if key_check in parts:
result[key] = value
return result
def check_value(value_check,dict):
result = {}
for key, value in dict.items():
if value == value_check:
result[key] = value
return result
def inspect_file(file_path):
str = open(file_path, 'r').read()
obj = flatten_json(json.loads(str))
filtered = check_key(args.key,obj)
if args.value:
filtered = check_value(args.value,filtered)
return filtered
def pretty_print(data):
for key, value in data.items():
print("Found in file {}:".format(key))
try:
for k,v in value.items():
print("Key: {}, Value: {}".format(k,v))
print("\n")
except:
print("This was unexpected")
pass
def main():
data = {}
files_in_dir = [f for f in os.listdir(args.path) if os.path.isfile(f)]
for file in files_in_dir:
if file.endswith((".json")):
found = inspect_file(os.path.join(args.path,file))
if found:
data[file] = found
if data:
pretty_print(data)
else:
print("Nothing found.")
if __name__ == "__main__":
main()