-
Notifications
You must be signed in to change notification settings - Fork 4
/
json2txttree.py
executable file
·86 lines (79 loc) · 2.93 KB
/
json2txttree.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
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
""" A python code to generate a tree or table for JSON data"""
from io import StringIO
__version__ = '1.0.0'
_branch_extend = '│ '
_branch_mid = '├─ '
_branch_last = '└─ '
_spacing = ' '
def _getHierarchy(jsonData, name='', file=None, _prefix='', _last=True):
""" Recursively parse json data to print data types """
# Handle object datatype
if isinstance(jsonData, dict):
name = name + ' (object)'
print(_prefix, _branch_last if _last else _branch_mid, \
name, sep="", file=file)
_prefix += _spacing if _last else _branch_extend
length = len(jsonData)
for i, key in enumerate(jsonData.keys()):
_last = i == (length - 1)
_getHierarchy(jsonData[key], '"' + key + '"', file, _prefix, _last)
# Handle array datatype
elif isinstance(jsonData, list):
name += ' (array)'
print(_prefix, _branch_last if _last else _branch_mid, \
name, sep="", file=file)
_prefix += _spacing if _last else _branch_extend
if jsonData:
_getHierarchy(jsonData[0], '', file, _prefix, _last=True)
else:
# Handle string datatype
if isinstance(jsonData, str):
name = name + ' (string)'
# Handle number datatype
else:
name = name + ' (number)'
print(_prefix, _branch_last if _last else _branch_mid, \
name, sep="", file=file)
def json2txttree(jsonData, file=None):
""" Output JSON data as tree to file or return as string """
if file == None:
messageFile = StringIO()
_getHierarchy(jsonData, file=messageFile)
message = messageFile.getvalue()
messageFile.close()
return message
else:
_getHierarchy(jsonData, file=file)
def json2txttable(jsonData, header=None, file=None):
""" Output JSON data as a Markkdown table to file or return as string """
# Get tree structure
treeFile = StringIO()
_getHierarchy(jsonData, file=treeFile)
tree = treeFile.getvalue()
treeFile.close()
# Convert to table
table = ''
for line in tree.splitlines():
items = line.split('"')
if len(items) > 1:
field = '`' + items[1] + '`'
datatype = items[2].replace('(','`')
datatype = datatype.replace(')','`')
table += '| ' + field + ' | ' + datatype + ' | - |\n'
# Add headers
if header == None:
header = '| Field | Data type | Details |\n' + \
'| ----- | --------- | ------- |\n'
table = header + table
if file == None:
return table
else:
print(table, file=file)
def setSymbols(branch_extend='│ ', branch_mid='├─ ', branch_last='└─ '):
""" Override symbols for the tree structure """
global _branch_extend
global _branch_mid
global _branch_last
_branch_extend = branch_extend
_branch_mid = branch_mid
_branch_last = branch_last