Home > database >  Rewrite json object list into key value pairs
Rewrite json object list into key value pairs

Time:07-14

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

CodePudding user response:

I hope I've understood you correctly:

lst = [
    {"kind": "admin#reports#activity", "other_key": 1},
    {"name": "api_name", "value": "caldav", "other_key": 2},
    {"name": "other_name", "intValue": 999, "other_key": 3},
]

out = []
for d in lst:
    if "kind" not in d:
        d[d.pop("name")] = d.pop("value") if "value" in d else d.pop("intValue")
    out.append(d)

print(out)

will transform the lst to:

[
    {"kind": "admin#reports#activity", "other_key": 1},
    {"other_key": 2, "api_name": "caldav"},
    {"other_key": 3, "other_name": 999},
]

CodePudding user response:

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:

  • Related