Search code examples
pythonjsongoogle-workspace

Rewrite json object list into key value pairs


I am trying to format oauth token logs pulled from Google Workspace API using python. The objects returned from the google API call use a mix of formats.

Some sections are formatted like "kind": "admin#reports#activity", which is preferred, while other sections are formtted like "name": "api_name", "value": "caldav". How can I rewrite the sections formatted like the second example to match the first example? "name": "api_name", "value": "caldav" would become "api_name" : "caldav".

Sample log returned (some sections have been redacted):

{"kind": "admin#reports#activity", "id": {"time": "2022-07-13T11:45:59.181Z", "uniqueQualifier": "<redacted>", "applicationName": "token", "customerId": "<redacted>"}, "etag": "<redacted>", "actor": {"email": "<redacted>", "profileId": "<redacted>"}, "ipAddress": "<redacted>", "events": [{"type": "auth", "name": "activity", "parameters": [{"name": "api_name", "value": "caldav"}, {"name": "method_name", "value": "caldav.calendars.report"}, {"name": "client_id", "value": "<redacted>"}, {"name": "num_response_bytes", "intValue": "165416"}, {"name": "product_bucket", "value": "CALENDAR"}, {"name": "app_name", "value": "<redacted>"}, {"name": "client_type", "value": "<redacted>"}]}]}

Thanks,

Dan


Solution

  • I tried using your sample log like this.

    import json
    d = {"kind": "admin#reports#activity", "id": {"time": "2022-07-13T11:45:59.181Z", "uniqueQualifier": "<redacted>", "applicationName": "token", "customerId": "<redacted>"}, "etag": "<redacted>", "actor": {"email": "<redacted>", "profileId": "<redacted>"}, "ipAddress": "<redacted>", "events": [{"type": "auth", "name": "activity", "parameters": [{"name": "api_name", "value": "caldav"}, {"name": "method_name", "value": "caldav.calendars.report"}, {"name": "client_id", "value": "<redacted>"}, {"name": "num_response_bytes", "intValue": "165416"}, {"name": "product_bucket", "value": "CALENDAR"}, {"name": "app_name", "value": "<redacted>"}, {"name": "client_type", "value": "<redacted>"}]}]}
    
    converted_dict = d["events"][0]["parameters"]
    ans = {}
    for i in converted_dict:
        ans[i['name']] =i.get('value') or i.get('intValue')
    print(ans)
    

    Output:

    {'api_name': 'caldav', 'method_name': 'caldav.calendars.report', 'client_id': '<redacted>', 'num_response_bytes': '165416', 'product_bucket': 'CALENDAR', 'app_name': '<redacted>', 'client_type': '<redacted>'}
    

    I hope this is what you wanted.

    You can also pass two keys to find value out of python dict like this:

    dictionary_name.get('value',dictionary_name.get('intValue'))
    

    more than two possible key then:👇 (First value will be 1st priority!)

    d.get('value',d.get('intValue',d.get(.......)))