I have a python object that looks like this. I am trying to parse this object and turn it to a human readable string which I need to put in the logs. How can I recursively loop through this considering the object could be nested dictionaries or nested lists or dictionaries inside lists inside dictionaries etc.
{"plugins":
[
{"Chrome PDF Viewer": "mhjfbmdgcfjbbpaeojofohoefgiehjai"},
{"Chrome PDF Viewer": "internal-pdf-viewer"},
{"Native Client": "internal-nacl-plugin"},
{"Shockwave Flash": "PepperFlashPlayer.plugin"},
{"Widevine Content Decryption Module": "widevinecdmadapter.plugin"}
]
}
I want to possibly serialize the above to look something like this
"plugins:
Chrome PDF Viewer": "mhjfbmdgcfjbbpaeojofohoefgiehjai,
Chrome PDF Viewer": "internal-pdf-viewer,
Native Client": "internal-nacl-plugin,
Shockwave Flash": "PepperFlashPlayer.plugin,
Widevine Content Decryption Module": "widevinecdmadapter.plugin"
My code so far [this works for nested dictionaries but I am not sure how I can alter this to support lists in the above object]:
result_str = ""
def dictionary_iterator(results):
global result_str
for key, value in results.items():
if isinstance(value, dict):
result_str = result_str + key + ": \n \t"
dictionary_iterator(value)
else:
result_str = result_str + key + ": " + str(value) + "\n"
return result_str
I have looked over possible answers but could not find a solution.