首页 > 解决方案 > 如何使用 python 删除字典对象?

问题描述

我有一本试图删除特定对象的字典。

{
  "authorizationQualifier": "00",
  "testIndicator": " ",
  "functionalGroups": [
    {
      "functionalIdentifierCode": "SC",
      "applicationSenderCode": "6088385400",
      "applicationReceiverCode": "3147392555",
      "transactions": [
        {
          "name": null,
          "transactionSetIdentifierCode": "832",
          "transactionSetControlNumber": "000000001",
          "implementationConventionReference": null,
          "segments": [
            {
              "BCT": {
                "BCT01": "PS",
                "BCT03": "2",
                "id": "BCT"
                     }
             }
           ]
         }
       ]
     }
   ]
}

我正在尝试删除对象的“段”列表,同时保留功能组和事务列表。

我试过了,

ediconverted = open("converted.json", "w")
with open('832.json','r') as jsonfile:
    json_content = json.load(jsonfile)

for element in json_content:
    element.pop('segments', None)

with open('converted.json', 'w') as data_file:
    json_content = json.dump(json_content, data_file)

标签: pythonjsondictionary

解决方案


对于这个特定的结构,我们将其命名为 d,以下将起作用:

del d["functionalGroups"][0]["transactions"][0]["segments"]

推荐阅读