This is the JSON i have
"""
{ "A":1,
"B":[
{
"C":
{
"D": "0"
},
"E":
{
"F": "1",
"G": [
{
"H": {
"I": 12,
"J": 21
}
}
]
}
}
]
}
"""
I want to print the JSON in the following way more likely in a tree fashion
{
'title': 'ROOT',
'key': '0',
'children': [
{
'title': 'A',
'key': 'A',
'children':[]
},
{
'title': 'B',
'key': 'B',
'children':[
{
'title': 'C',
'key': 'C',
'children':[
{
'title': 'D',
'key': 'D',
'children':[]
}
]
},
{
'title': 'E',
'key': 'E',
'children':[
{
'title': 'F',
'key': 'F',
'children':[]
},{
'title': 'G',
'key': 'G',
'children':[
{
'title': 'H',
'key': 'H',
'children':[
{
'title': 'I',
'key': 'I',
'children':[]
},
{
'title': 'J',
'key': 'J',
'children':[]
}
]
}
]
}
]
}
]
}
]
}
here is the reference to the above printing pattern link !
Here is the code i tried
import json
json_data = json.loads("""
{ "A":1,
"B":[
{
"C":
{
"D": "0"
},
"E":
{
"F": "1",
"G": [
{
"H": {
"I": 12,
"J": 21
}
}
]
}
}
]
}
""")
__version__ = '1.0.0'
_branch_extend = '│ '
_branch_mid = '├─ '
_branch_last = '└─ '
_spacing = ' '
last_items = []
main_json_obj = {
'title': """ ROOT """, 'key': 'ROOT',
'children': []
}
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
print(_prefix, _branch_last if _last else _branch_mid, \
name, sep="", file=file)
if _last:
last_items.append([{'title': name, 'key': name, 'children': []}])
main_json_obj['children'] += [{'title': name, 'key': name, 'children': []}]
_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)
if _last:
last_items.append([{'title': name, 'key': name, 'children': []}])
main_json_obj['children'] += [{'title': name, 'key': name, 'children': []}]
_prefix += _spacing if _last else _branch_extend
_getHierarchy(jsonData[0], '', file, _prefix, _last=True)
else:
# Handle string datatype
if isinstance(jsonData, str):
name = name
if _last:
last_items.append([{'title': name, 'key': name, 'children': []}])
main_json_obj['children'] += [{'title': name, 'key': name, 'children': []}]
# Handle number datatype
else:
name = name
if _last:
last_items.append([{'title': name, 'key': name, 'children': []}])
main_json_obj['children'] += [{'title': name, 'key': name, 'children': []}]
print(_prefix, _branch_last if _last else _branch_mid, \
name, sep="", 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
# print(json_data)
_getHierarchy(json_data)
print(" ")
print(" ")
print("--------------------NON LAST ELEMENTS ---------------------")
print(" ")
print(" ")
print(main_json_obj)
print(" ")
print(" ")
print("^^^^^^^^^LAST ELEMENTS AND NESTED-ROOT ELEMENTS^^^^^^^^^^^^")
print(" ")
print(" ")
print(last_items)
this is its output at the moment!
└─
├─ "A"
└─ "B"
└─
├─ "C"
│ └─ "D"
└─ "E"
├─ "F"
└─ "G"
└─
└─ "H"
├─ "I"
└─ "J"
--------------------NON LAST ELEMENTS ---------------------
{'title': ' ROOT ', 'key': 'ROOT', 'children': [{'title': '', 'key': '', 'children': []}, {'title': '"A"', 'key': '"A"', 'children': []}, {'title': '"B"', 'key': '"B"', 'children': []}, {'title': '', 'key': '', 'children': []}, {'title': '"C"', 'key': '"C"', 'children': []}, {'title': '"D"', 'key': '"D"', 'children': []}, {'title': '"E"', 'key': '"E"', 'children': []}, {'title': '"F"', 'key': '"F"', 'children': []}, {'title': '"G"', 'key': '"G"', 'children': []}, {'title': '', 'key': '', 'children': []}, {'title': '"H"', 'key': '"H"', 'children': []}, {'title': '"I"', 'key': '"I"', 'children': []}, {'title': '"J"', 'key': '"J"', 'children': []}]}
^^^^^^^^^LAST ELEMENTS AND NESTED-ROOT ELEMENTS^^^^^^^^^^^^
[[{'title': '', 'key': '', 'children': []}], [{'title': '"B"', 'key': '"B"', 'children': []}], [{'title': '', 'key': '', 'children': []}], [{'title': '"D"', 'key': '"D"', 'children': []}], [{'title': '"E"', 'key': '"E"', 'children': []}], [{'title': '"G"', 'key': '"G"', 'children': []}], [{'title': '', 'key': '', 'children': []}], [{'title': '"H"', 'key': '"H"', 'children': []}], [{'title': '"J"', 'key': '"J"', 'children': []}]]
Now the problem is how to deal with regions where things go nested !
any help would be really appreciated!
Thanks in advance.
from How to iterate through JSON to print in a formatted way
No comments:
Post a Comment