I have a json tree at hand, I want to reformat it.
**The input is somewhat like : **
[
{
"archive":{
"LOGS":{
"_files":[
"zomp_download_20220601170001.log",
"zomp_download_20220626170002.log"
]
},
"SUMMARY":{
"_files":[
"SUMMARY_zomp_report_2022-04-01.csv",
"SUMMARY_zomp_report_2022-06-25.csv"
]
},
"_files":[
"lexter_report_2022-04-01.csv",
"lexter_report_2022-06-25.csv"
]
}
}
]
**and the expected output : **
[
{
"text": "archive",
"nodes":[
{
"text":"LOGS",
"nodes":[
{
"text":"_files",
"nodes":[
{
"text":"zomp_download_20220601170001.log"
},
{
"text":"zomp_download_20220626170002.log"
}
]
}
]
},
{
"text": "SUMMARY",
"nodes":[
{
"text":"SUMMARY_zomp_report_2022-04-01.csv"
},
{
"text":"SUMMARY_zomp_report_2022-06-25.csv"
}
]
},
{
"text":"lexter_report_2022-04-01.csv"
},
{
"text":"lexter_report_2022-06-25.csv"
}
]
}
]
I tried to iterate over it, but I am unable to think of any logic to do so. I tried to put the input in a file and read it as a json.
import json
f = open(file)
config_file = json.load(f)
def traverse_dict(d):
for d_key in d.keys():
if isinstance(d[d_key], dict):
new_dict = {}
new_dict["node"] = [d[d_key]]
traverse_dict(d[d_key])
else:
new_dict = {}
new_dict["text"] = d[d_key]
for i in config_file:
traverse_dict(i)
print(i)
I need help in python to convert the json input as the json output. Any help/indicative logic/ examples will be very helpful to me.
2
Answers
You can make a recursive function to achieve your goal :
I think this simple 1:1 transformation should do it –
Decode the JSON, remap it, encode it, print it –
The remapped output –