基于以下JSON
"Records": [
{
"Action_Id": "5542.3478.137599",
"SKU": "DSOLSSREDGUM_V",
"SubId1": "1011l4dIyBtf",
},
{
"Action_Id": "5542.3477.118504",
"SKU": "DSOLSSREDGUM_V",
"SubId1": "1101l4dI2VPX",
},
{
"Action_Id": "5542.3477.118504",
"SKU": "PARCMEPGR15_C6",
"SubId1": "1101l4dI2VPX",
},
{
"Action_Id": "5542.3477.117975",
"SKU": "DSOLSSREDGUM_V",
"SubId1": "1101l4dI2Hwz",
},
{
"Action_Id": "5542.3477.117852",
"SKU": "DSOLSSREDGUM_V",
"SubId1": "1101l4dI2EZK",
}
]
}
我想根据Action_Id
对元素进行分组。因此,使用上述数据,输出将是
{"Records": [
{
"Action_Id:":"5542.3477.118504",
"Items":
[
{
"SKU":"DSOLSSREDGUM_V",
"SubId1":"1101l4dI2VPX"
},
{
"SKU":"PARCMEPGR15_C6",
"SubId1":"1101l4dI2VPX"
}
],
},
{
"Action_Id":"5542.3478.137599",
"Items":
[
{
"SKU":"DSOLSSREDGUM_V",
"SubID1":"1011l4dIyBtf"
}
],
},
{
"Action_Id":"5542.3477.117975",
"Items":
[
{
"SKU":"DSOLSSREDGUM_V",
"SubID1":"1101l4dI2Hwz"
}
],
},
{
"Action_Id":"5542.3477.117852",
"Items":
[
{
"SKU":"DSOLSSREDGUM_V",
"SubID1":"1101l4dI2EZK"
}
],
}
]
}
答案 0 :(得分:1)
尝试这种方式:
import collections,itertools
a_dict = { "Records": [
{
"Action_Id": "5542.3478.137599",
"SKU": "DSOLSSREDGUM_V",
"SubId1": "1011l4dIyBtf",
},
{
"Action_Id": "5542.3477.118504",
"SKU": "DSOLSSREDGUM_V",
"SubId1": "1101l4dI2VPX",
},
{
"Action_Id": "5542.3477.118504",
"SKU": "PARCMEPGR15_C6",
"SubId1": "1101l4dI2VPX",
},
{
"Action_Id": "5542.3477.117975",
"SKU": "DSOLSSREDGUM_V",
"SubId1": "1101l4dI2Hwz",
},
{
"Action_Id": "5542.3477.117852",
"SKU": "DSOLSSREDGUM_V",
"SubId1": "1101l4dI2EZK",
}
]
}
temp_dict = collections.defaultdict(list)
for key, group in itertools.groupby(a_dict['Records'], lambda item: item["Action_Id"]): #group by Action_Id
for e in group:
del e['Action_Id'] #remove key,value for key = Action_Id
temp_dict[key].append(e)
result_list= [{'Action_Id':key, 'Items': value} for key,value in temp_dict.items()]
result_dict = {"Records":result_list}
result_dict
result_dict
将是:
{'Records': [{'Action_Id': '5542.3478.137599',
'Items': [{'SKU': 'DSOLSSREDGUM_V', 'SubId1': '1011l4dIyBtf'}]},
{'Action_Id': '5542.3477.117975',
'Items': [{'SKU': 'DSOLSSREDGUM_V', 'SubId1': '1101l4dI2Hwz'}]},
{'Action_Id': '5542.3477.118504',
'Items': [{'SKU': 'DSOLSSREDGUM_V', 'SubId1': '1101l4dI2VPX'},
{'SKU': 'PARCMEPGR15_C6', 'SubId1': '1101l4dI2VPX'}]},
{'Action_Id': '5542.3477.117852',
'Items': [{'SKU': 'DSOLSSREDGUM_V', 'SubId1': '1101l4dI2EZK'}]}]}
答案 1 :(得分:1)
这种方法可行:
# create new dict with necessary keys
new_json = {'Records':[{'Action_Id':v} for v in set([i['Action_Id'] for i in original_json['Records']])]}
# iterate over the previous dict to find items values
# while filtering out Action_Id values, which are now in the keys
for i in new_json['Records']:
i['items'] = [{k:v for k, v in j.iteritems() if k != 'Action_Id'} for j in original_json['Records'] if i['Action_Id'] == j['Action_Id']]
# convert to json string
new_json = json.dumps(new_json)
结果:
'{
"Records": [{
"items": [{
"SKU": "DSOLSSREDGUM_V",
"SubId1": "1011l4dIyBtf"
}],
"Action_Id": "5542.3478.137599"
}, {
"items": [{
"SKU": "DSOLSSREDGUM_V",
"SubId1": "1101l4dI2Hwz"
}],
"Action_Id": "5542.3477.117975"
}, {
"items": [{
"SKU": "DSOLSSREDGUM_V",
"SubId1": "1101l4dI2VPX"
}, {
"SKU": "PARCMEPGR15_C6",
"SubId1": "1101l4dI2VPX"
}],
"Action_Id": "5542.3477.118504"
}, {
"items": [{
"SKU": "DSOLSSREDGUM_V",
"SubId1": "1101l4dI2EZK"
}],
"Action_Id": "5542.3477.117852"
}]
}'