-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJsonHandler.py
More file actions
44 lines (37 loc) · 1.11 KB
/
Copy pathJsonHandler.py
File metadata and controls
44 lines (37 loc) · 1.11 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
import json
import io
import requests
class JsonHandler(dict):
### This is JsonHandler for easy Json handling. ###
def URLParserJson(url) :
r = requests.get(url)
return r
def URLParserJsonDict(url) :
r = requests.get(url)
datadict = r.json()
return datadict
def OpenFile(filepath) :
f = open(filepath, 'r')
f = f.read()
return f
def OpenJsonFileConvertToDict(filepath) :
f = open(filepath, 'r')
JsonDict = json.loads(f.read())
f.close()
return JsonDict
def getValue(self, path, default = None):
keys = path.split("/")
val = None
for key in keys:
if val:
if isinstance(val, list):
val = [ v.get(key, default) if v else None for v in val]
else:
val = val.get(key, default)
else:
val = dict.get(self, key, default)
if not val:
break;
return val
# Example :
# print (JsonHandler(JsonDict).getValue('results/description')