简体   繁体   English

将json对象列表重写为键值对

[英]Rewrite json object list into key value pairs

I am trying to format oauth token logs pulled from Google Workspace API using python.我正在尝试使用 python 格式化从 Google Workspace API 提取的 oauth 令牌日志。 The objects returned from the google API call use a mix of formats.从 google API 调用返回的对象使用多种格式。

Some sections are formatted like "kind": "admin#reports#activity" , which is preferred, while other sections are formtted like "name": "api_name", "value": "caldav" .某些部分的格式为"kind": "admin#reports#activity" ,这是首选,而其他部分的格式为"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" . "name": "api_name", "value": "caldav"会变成"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

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},
]

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

print(lst)

will transform the lst to:lst转换为:

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

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:您还可以传递两个键来从 python dict 中查找值,如下所示:

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(.......)))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM