-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathjson_convert.py
55 lines (48 loc) · 1.74 KB
/
json_convert.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
import json
from clikpython.utils import message, debug, printy
#sample api keys
api_val = {
'ADMIN_KEY':'exampleadmin123',
'ADMIN_USERNAME':'example@admin',
'ADMIN_PASSWORD':'Darwin@Example2506',
'TWITTER_ACCESS_TOKEN':'1234614-4Du6ZlFtVOpNMgtwIKjRe4wzwxHejcDRaxjNmAU',
'INSTA_API_KEY':'Qhsh6yihijy3e8',
'YOUTUBE_API_KEY':'83nicciwdcIIUNij',
'FACEBOOK_API_KEY_SECRET':'7jewkdjwenbnsH',
'INSTA_API_KEY_SECRET':'Qhsh6fjkdsbfky3e8',
'YOUTUBE_API_KEY_SECRET':'8djkfhjsndcIIUNij',
'FACEBOOK_API_KEYS_SECRET':'7jewdsjnfdnjnfssnsH'
}
#function to convert JSON to dictionary and print
def jsonToDict(filename):
try:
with open(filename,'r') as fh:
json_str = fh.read()
json_value = json.loads(json_str)
return json_value
except:
return False
#function to convert dictionary to JSON
def dictToJson(inputDict, filename, indents=2):
try:
with open(filename, 'w') as fh2:
fh2.write(json.dumps(inputDict, indent=indents, sort_keys=True))
message.success('Keys stored successfully')
return True
except Exception as e:
message.error('Couldn\'t write to JSON')
debug.error(str(e))
return False
# function to format and display JSON data
def display_json (filename, display_indent=4):
json_val = jsonToDict(filename)
if json_val:
message.success(filename + ' loaded successfully')
printy (json_val, indentation=display_indent)
else:
message.error('JSON couldn\'t be loaded')
debug.error('JSON to Dict Failed')
#calling the functions
#print(jsonToDict('test_data/test_api_keys.json'))
# dictToJson(api_val,'test_data/test_api_keys.json')
#display_json ('test_data/test_api_keys.json')