Convert list of objects to attributes

Google Admin SDK Reports API Activities.list() method returns activity events. In the relevant portion of the records return an object would look like this…

{
   ...,
   "events": [
      {
         "type": "call",
         "event": "call_ended",
         "paramaters": [
            {
               "name": "network_estimated_upload_kbps_mean",
               "intValue": "10"
            },
            {
               "name": "meeting_code",
               "value": "ABCDEFGH"
            },
            {
               "name": "is_external",
               "boolValue": true
            }
         ]
   ],
   ...
}

I need to load these events into a database table. I’m looking for a pythonic AND fast way to convert the parameters list to an object with properties that once loaded, will be easier to process in . So parameters would become…

{
  "type": "call",
  "event": "call_ended",
  "parameters": {
    "network_estimated_upload_kbps_mean": 10,
    "meeting_code": "ABCDEFGH",
    "is_external": true
  }
}

I thought I would use something like a list comprehension or lambda, but I’m not great at python and the data types are encoded into the property names of the objects … and I couldn’t figure it out.

Answer

You can use defaultdict to pass values of original paramaters key, then pass that defaultdict as a value of new dictionary:

from collections import defaultdict
true = True

data = {
    "type": "call",
    "event": "call_ended",
    "paramaters": [
        {
            "name": "network_estimated_upload_kbps_mean",
            "intValue": "10"
            },
        {
            "name": "meeting_code",
            "value": "ABCDEFGH"
            },
        {
            "name": "is_external",
            "boolValue": true
            }
        ]
    }

# form defaultdict with key being first value of inner dictionary 
# and value being second value of inner dictionary
d = defaultdict(dict)
[d.update({list(p.values())[0]: list(p.values())[1]}) for p in data['paramaters']]
# -> defaultdict(<class 'dict'>, {'network_estimated_upload_kbps_mean': '10', 'meeting_code': 'ABCDEFGH', 'is_external': True})

new_dict = {k: v for k, v in data.items() if k != 'paramaters'}
new_dict['paramaters'] = dict(d)

new_dict would look like this:

{'type': 'call', 'event': 'call_ended', 'paramaters': {'network_estimated_upload_kbps_mean': '10', 'meeting_code': 'ABCDEFGH', 'is_external': True}}