Python:嵌套 JSON 列表

这是我的 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"

            ]

        }]

    }]

}]


jeck猫
浏览 135回答 1
1回答

偶然的你

怎么样: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']}]}]}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python