这是我的 JSON 文档中的示例 JSON 对象:[{"_id": "COVERAGE_0025","coverageName": "windStormHailDeductible","coverageResponse": "15000","insuranceLine": "COMMERCIAL","coverageCategory": "DEDUCTIBLE","splitCoverages": null,"carriers": [{ "carrierId": "CNINCO", "states": null, "excludedStates": [ "FL" ], "industries": null, "excludedIndustries": null, "products": [ "BOP", "WC" ]}]我需要将除了coverageName和_id之外的所有内容分组coverages。这是我想要的输出:[{ "_id": "COVERAGE_0025", "coverageName": "windStormHailDeductible", "coverages": [{ "coverageResponse": "15000", "insuranceLine": "COMMERCIAL", "coverageCategory": "DEDUCTIBLE", "splitCoverages": null, "carriers": [{ "carrierId": "CNINCO", "states": null, "excludedStates": [ "FL" ], "industries": null, "excludedIndustries": null, "products": [ "BOP", "WC" ] }] }]}]
1 回答
偶然的你
TA贡献1841条经验 获得超3个赞
怎么样:
fixed = ["_id","coverageName"]
d_new = {}
for elem in fixed:
d_new[elem] = d[elem]
d_new["coverages"] = [{}]
for elem in d.keys():
if elem not in fixed:
d_new["coverages"][0][elem] = d[elem]
输出:
{'_id': 'COVERAGE_0025',
'coverageName': 'windStormHailDeductible',
'coverages': [{'coverageResponse': '15000',
'insuranceLine': 'COMMERCIAL',
'coverageCategory': 'DEDUCTIBLE',
'splitCoverages': 'null',
'carriers': [{'carrierId': 'CNINCO',
'states': 'null',
'excludedStates': ['FL'],
'industries': 'null',
'excludedIndustries': 'null',
'products': ['BOP', 'WC']}]}]}
添加回答
举报
0/150
提交
取消