0

I'm flattening a nessted dict with this structure:

[{'path': 'messageType', 'value': 'position'}, {'path': 'fcntup', 'value': 3320}]

to have something like:

{'messageType': 'position', 'fcntup': 3320}

using this code:

myDict= {x['path']: x['value'] for x in message['data']}

however, sometimes, some of the dict dosen't has the key 'value', like:

[{'path': 'messageType', 'value': 'position'}, {'path': 'fcntup', 'value': 3320}, {'path': 'deviceType'}, {'path': 'uplinkId'}]

and when this happens, I got a KeyError and that doesn't allow me continue properly becasue i need to handle this and have a final dict like:

{'messageType': 'position', 'fcntup': 3320, 'deviceType': null, 'uplinkId': null}

Any idea how I could do this? I tried using a try-catch logic but doesn't works.

Thanks for the help

Reco Jhonatan
  • 1,503
  • 4
  • 23
  • 35

0 Answers0