I have the following piece of code.
json_tree = {
"MyHouseHold": {
"Kitchen": {
"@myid": "1.3.61",
"BakingOven": {
"@myid": "1",
"InfoList": {
"Notice": {
"@noticelist": "10/TimeToCookTheChicken/20/TimetoCookSalmon",
"@myid": "2"
}
},
"state": "0",
"Tid": "3"
},
"state": "0",
"Tid": "2"
},
"Tid": "1",
"state": "0"
}
}
def yield_output(d):
container = {'Tid': d['Tid'],
'state': d.get('state')
}
temp_result = d.get('InfoList', {}).get('Notice')
# If temp_result is a dict (not None), then we can get `@noticelist`
if temp_result is not None:
container['noticelist'] = temp_result.get('@noticelist')
container['state'] = d.get('state')
return container
def get_output(d, id):
# Iterate over the json_tree to return the
# 'Tid' and 'state' and 'noticelist' information for the requested 'Tid'
if isinstance(d, dict) and d.get('Tid') == id:
yield yield_output(d)
for i in getattr(d, "values", lambda: [])():
yield from get_based_on_id(i, id)
So, basically for a given Tid
, when the get_output function is called the output is as the following.
key_list = list(get_based_on_id(json_tree, 3))
jsonify(key_list)
"Jsonify Output":
{
"Tid": "3",
"state": "0",
"noticelist": "10/TimeToCookTheChicken/20/TimetoCookSalmon"
}
The issue is that I want to be able to modify the noticelist
to have an output which looks sometime like the following.
"noticelist": "{10,TimeToCookTheChicken},{20,TimetoCookSalmon}"
Thus my final output should be something like the following:
"Jsonify Output":
{
"Tid": "3",
"state": "0",
"noticelist": "{10,TimeToCookTheChicken},{20,TimetoCookSalmon}"
}
I am unsure on how to modify the def yield_output(d)
to reflect the desired output.
Any help would be appreciated.