forked from kumaya/python-programs
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjson_decoder.py
More file actions
Latest commit
44 lines (34 loc) · 1.08 KB
/
Copy pathjson_decoder.py
File metadata and controls
44 lines (34 loc) · 1.08 KB
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
importsimplejsonasjson
deffind_values(id, id2, obj):
results= []
def_find_values(id, obj):
try:
forkey, valueinobj.iteritems():
ifvalue==id:
results.append(obj)
elifnotisinstance(value, basestring):
_find_values(id, value)
exceptAttributeError:
pass
try:
foriteminobj:
ifnotisinstance(item, basestring):
_find_values(id, item)
exceptTypeError:
pass
ifnotisinstance(obj, basestring):
_find_values(id, obj)
def_find_hostname(id, obj):
foriteminobj:
ifitem.get(id2):
returnitem.get(id2)
return_find_hostname(id, results)
deffinal_parser(id, obj):
foriteminobj:
ifitem.get('hostname'):
returnitem.get('hostname')
if__name__=="__main__":
data= {"name": "mayank", "hostname": "mayank-test"}
some_dict=find_values('mayank', "hostname", data)
printsome_dict