forked from kumaya/python-programs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
json_decoder.py
44 lines (34 loc) · 1.08 KB
/
json_decoder.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
# json decoder
import simplejson as json
def find_values(id, id2, obj):
results = []
def _find_values(id, obj):
try:
for key, value in obj.iteritems():
if value == id:
results.append(obj)
elif not isinstance(value, basestring):
_find_values(id, value)
except AttributeError:
pass
try:
for item in obj:
if not isinstance(item, basestring):
_find_values(id, item)
except TypeError:
pass
if not isinstance(obj, basestring):
_find_values(id, obj)
def _find_hostname(id, obj):
for item in obj:
if item.get(id2):
return item.get(id2)
return _find_hostname(id, results)
def final_parser(id, obj):
for item in obj:
if item.get('hostname'):
return item.get('hostname')
if __name__ == "__main__":
data = {"name": "mayank", "hostname": "mayank-test"}
some_dict = find_values('mayank', "hostname", data)
print some_dict