为了账号安全,请及时绑定邮箱和手机立即绑定

Python:嵌套 JSON 列表

Python:嵌套 JSON 列表

jeck猫 2023-07-05 15:36:13
这是我的 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']}]}]}


查看完整回答
反对 回复 2023-07-05
  • 1 回答
  • 0 关注
  • 134 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信