亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

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 關注
  • 168 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號