实际上使用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,你有什么想法吗?
答案 0 :(得分:0)
我确实喜欢这个。看看吧。
import json
json_str = """
[
{
"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"
}
]
"""
data_set = json.loads(json_str)
data_list = []
for data in data_set:
title = data["productTitle"]
exist = False
for existing_data in data_list:
if title in existing_data:
exist = True
existing_data[title] += 1
if data["apiName"] in existing_data["api-activity"]:
existing_data["api-activity"][data["apiName"]] += 1
else:
existing_data["api-activity"][data["apiName"]] = 1
if not exist:
new = dict()
new[data["productTitle"]] = 1
new["api-activity"] = dict()
new["api-activity"][data["apiName"]] = 1
data_list.append(new)
print(json.dumps(data_list))
答案 1 :(得分:0)
使用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
}
}
答案 2 :(得分:0)
你的第一个问题是json.load(和json.loads)只会从文件中获取一个元素。要修复此问题,您需要将其转换为列表。
[
{
"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_obj ['calls']。通过删除访问者,您将获得以下代码:
import json
from collections import Counter
with open('events1.json') as json_data:
json_obj = json.load(json_data) # Read the JSON file
c = Counter(player['productTitle'] for player in json_obj)
with open('output.json', 'w') as f:
f.write(json.dumps(c, indent=4)) # Parse and write the file
print("Translation of JSON");
这会在output.json中产生以下输出:
{
"Product": 4,
"Suitability": 1
}