gpt4 book ai didi

python - 如何在 Python 中使用多个键解析和排序 JSON

转载 作者:太空宇宙 更新时间:2023-11-04 04:54:25 25 4
gpt4 key购买 nike

实际上是用 JSON 进行训练,试图了解如何使用两个不同的键解析新的 JSON。我想对一些日志进行排序以实现数据可视化目的。

我的数据 JSON

{
"productTitle": "Product",
"apiName": "soapwebservice"
"statusCode": "200 OK"
},
{
"productTitle": "Product",
"apiName": "productionservice",
"statusCode": "200 OK"
},
{
"productTitle": "Product",
"apiName": "firstapi",
"statusCode": "200 OK"
},
{
"productTitle": "Product",
"apiName": "firstapi",
"statusCode": "200 OK"
},
{
"productTitle": "Suitability",
"apiName": "suitability-api",
"statusCode": "200 OK"
}

预期的输出 JSON:

{
"Product": 4,
"api-activity": {
"soapwebservice": 1,
"productionservice": 1,
"firstapi": 2
}
}
{
"Suitability": 1,
"api-activity": {
"suitability-api": 1,
}
}

这是我第一个解析和计算第一个键的代码:

import json
from collections import Counter

with open('events1.json') as json_data:
json_obj = json.load(json_data) # Read the JSON file

# print(json_obj['calls'][0]['appName']); #Example JSON Extract
c = Counter(player['productTitle'] for player in json_obj['calls'])

with open('output.json', 'w') as f:
f.write(json.dumps(c, indent=4)) # Parse and write the file

print("Translation of JSON");

我正在寻找一种获取预期 JSON 的方法,可能使用此处解释的循环:parse JSON values by multilevel keys但是我无法按预期获取JSON,你有什么想法吗?

最佳答案

使用itertools.groupby()按所需属性对元素进行分组。首先按产品标题分组,然后在每个结果组中再次对元素进行分组,但现在按 api 名称分组。所需的计数器只是每个结果组中元素的数量:

import itertools
import json

def by_product_title(data):
return data['productTitle']

def by_api_name(data):
return data['apiName']

json_str = '''
[
{
"productTitle": "Product",
"apiName": "soapwebservice",
"statusCode": "200 OK"
},
{
"productTitle": "Product",
"apiName": "productionservice",
"statusCode": "200 OK"
},
...
]
'''

data = json.loads(json_str)
grouped_by_product_title = itertools.groupby(sorted(data, key=by_product_title), by_product_title)
for product, group in grouped_by_product_title:
elements = list(group)
grouped_by_api_name = itertools.groupby(sorted(elements, key=by_api_name), by_api_name)
api_activity = {key: len(list(val)) for key, val in grouped_by_api_name}
output = {product: len(elements), 'api-activity': api_activity}
json_output = json.dumps(output, sort_keys=True, indent=4)
print(json_output)

输出:

{
"Product": 4,
"api-activity": {
"firstapi": 2,
"productionservice": 1,
"soapwebservice": 1
}
}
{
"Suitability": 1,
"api-activity": {
"suitability-api": 1
}
}

关于python - 如何在 Python 中使用多个键解析和排序 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47398054/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com