-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexplore_json.py
More file actions
executable file
·109 lines (89 loc) · 3.37 KB
/
explore_json.py
File metadata and controls
executable file
·109 lines (89 loc) · 3.37 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
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#!/home/rmhines/anaconda3/bin/python3
# -*- coding: utf-8 -*-
"""
Use kid exploration functions to diagram json files starting with either
a dict or list node. Created for use with Trello json exports.
@author: quidscio
License: unlicense
01.19.19 rmh v1
"""
# imports
import json
import argparse
import datetime
# identify json file to parse
json_file = 'export-test.json'
def kidL(jobj, prefix=""):
""" Explore list object (for debug) """
for j in jobj:
#print(prefix,"t({}) v({})".format(type(j),j))
if type(j) is dict:
#tprefix = prefix + " -d-{}--".format(j)
if len(j) == 0:
tprefix = prefix + " -dL-0LEN-{}--".format(j)
print(tprefix)
else:
tprefix = prefix + " -dL----"
tmp = kidD(j, prefix=tprefix)
elif type(j) is str:
print(prefix,"{}".format(j))
else:
raise Exception(", {} print not implemented".format(type(j)))
#==============================================================================
def kidD(jobj, prefix=""):
""" Explore dict object (for debug) - generally top-level of json object """
# print("== KidD %s"%jobj)
robj = []
for k,v in jobj.items():
# print("__ k {} t(v) {}".format(k,type(v)))
if type(v) is str:
print(prefix,"{}, {}".format(k,v))
elif isinstance(v, datetime.date):
print(prefix,"{}, {}".format(k,str(v)))
elif type(v) is dict:
if len(v) == 0:
tprefix = prefix + " -dD-0LEN-{}--".format(k)
print(tprefix)
else:
tprefix = prefix + " -dD-{}--".format(k)
tmp = kidD(v, prefix=tprefix)
elif type(v) is list:
if len(v) == 0:
tprefix = prefix + " -lD-0LEN-{}--".format(k)
print(tprefix)
else:
tprefix = prefix + " -lD-{}--".format(k)
tmp = kidL(v, prefix=tprefix)
elif type(v) is int:
print(prefix,"{}, {}".format(k,v))
elif type(v) is float:
print(prefix,"{}, {}".format(k,v))
elif type(v) is bool:
print(prefix,"{}, {}".format(k,v))
elif v is None:
print(" 00","{}, {}".format(k,None))
else:
#print(prefix,", {} print not implemented".format(type(v)))
raise Exception(prefix,", {} print not implemented".format(type(v)))
#robject.append(k)
# Yes...this returns nothing but could..
return robj
#==============================================================================
if __name__ == "__main__":
print("== Layout json for input file, ", end="")
# setup argument parser (p)
p = argparse.ArgumentParser(description="Explore json files using kidD/kidL functions")
p.add_argument('-f', '--file', help='json input file')
args = p.parse_args()
if args.file:
json_file = args.file
print(json_file)
# read the file
with open(json_file) as data_file:
jdata = json.load(data_file)
# explore at any level by providing a specific dict or list
tmp = kidD(jdata)
#print("\n==TMP ",tmp)
# test ability to focus on a node within the json
print("== Test on a subnode")
tmp = kidL(jdata['memberships'])