如何在Python中从JSON中删除空键/值对

时间:2015-07-12 00:06:43

标签: python json dictionary

如果Name字段为空且Item_2字段为< Name字段为空,我正在构建一个带有键/值对的JSON,我将其发送到服务器0,然后我想删除lResults字段。

我正在处理多达10个嵌套词典,但只有字典2-10或1-9 [索引]才符合此标准。

lResults[0]['SRData']['ListOfLa311DeadAnimalRemoval']['DeadAnimalRemoval'][0]['Name']是我的结果列表,它被转储为JSON格式,当我打印 dL311 = dict() d = dict() d.setdefault("DriverFirstName",last_edited_user ) d.setdefault("DriverLastName","Aguilar" ) d.setdefault("LastUpdatedBy", "SANSTAR1") d.setdefault("DACType", dar_item_1) d.setdefault("Type", "Dead Animal Removal") d.setdefault("Name", dar_uid_1 ) d.setdefault("DACItemCount", dar_qyt_1) l311.append(d) dL311 = dict() d = dict() d.setdefault("DriverFirstName",last_edited_user ) d.setdefault("DriverLastName","Aguilar" ) d.setdefault("LastUpdatedBy", "SANSTAR1") d.setdefault("DACType", dar_item_2) d.setdefault("Type", "Dead Animal Removal") d.setdefault("Name", dar_uid_2) d.setdefault("DACItemCount", dar_qyt_2) l311.append(d) d = dict() d.setdefault("DriverFirstName",last_edited_user ) d.setdefault("DriverLastName","Aguilar" ) d.setdefault("LastUpdatedBy", "SANSTAR1") d.setdefault("DACType", dar_item_3) d.setdefault("Type", "Dead Animal Removal") d.setdefault("Name", dar_uid_3) d.setdefault("DACItemCount", dar_qyt_3) l311.append(d) d = dict() d.setdefault("DriverFirstName",last_edited_user ) d.setdefault("DriverLastName","Aguilar" ) d.setdefault("LastUpdatedBy", "SANSTAR1") d.setdefault("DACType", dar_item_4) d.setdefault("Type", "Dead Animal Removal") d.setdefault("Name", dar_uid_4) d.setdefault("DACItemCount", dar_qyt_4) l311.append(d) d = dict() d.setdefault("DriverFirstName",last_edited_user ) d.setdefault("DriverLastName","Aguilar" ) d.setdefault("LastUpdatedBy", "SANSTAR1") d.setdefault("DACType", dar_item_5) d.setdefault("Type", "Dead Animal Removal") d.setdefault("Name", dar_uid_5) d.setdefault("DACItemCount", dar_qyt_5) l311.append(d) dL311 = dict() dL311.setdefault("DeadAnimalRemoval", l311) dResult.setdefault("ListOfLa311DeadAnimalRemoval",dL311) lResults.append({"MetaData": {}, "SRData": dResult}) 时只打印值,我没有看到空字符串,但是当lResults被转储到JSON中时我看到空白的字符串。

10个字典中的每个字典中的变量可能是空白,也可能不是空白,但是我发送此JSON的服务器要么是期望名字字段要么是什么,要么名称字段对每个字典都是唯一的。

这是我的代码;

if "Name" == " " and DacItemCount < 0:
then remove `Name` key and value

作为JSON转储的lResults的结果,我想删除所有我想构建condtion的键,其中指出:

{
            "MetaData": {}, 
            "SRData": {
                "ListOfLa311DeadAnimalRemoval": {
                    "DeadAnimalRemoval": [
                        {
                            "DACItemCount": "0", 
                            "DACType": " ", 
                            "DriverFirstName": "Esri_Anonymous", 
                            "DriverLastName": "Aguilar", 
                            "LastUpdatedBy": "SANSTAR1", 
                            "Name": "070920151120552101", 
                            "Type": "Dead Animal Removal"
                        }, 
                        {
                            "DACItemCount": "0", 
                            "DACType": " ", 
                            "DriverFirstName": "Esri_Anonymous", 
                            "DriverLastName": "Aguilar", 
                            "LastUpdatedBy": "SANSTAR1", 
                            "Name": " ", 
                            "Type": "Dead Animal Removal"
                        }, 
                        {
                            "DACItemCount": "0", 
                            "DACType": " ", 
                            "DriverFirstName": "Esri_Anonymous", 
                            "DriverLastName": "Aguilar", 
                            "LastUpdatedBy": "SANSTAR1", 
                            "Name": " ", 
                            "Type": "Dead Animal Removal"
                        }, 
                        {
                            "DACItemCount": "0", 
                            "DACType": " ", 
                            "DriverFirstName": "Esri_Anonymous", 
                            "DriverLastName": "Aguilar", 
                            "LastUpdatedBy": "SANSTAR1", 
                            "Name": " ", 
                            "Type": "Dead Animal Removal"
                        }, 
                        {
                            "DACItemCount": "0", 
                            "DACType": " ", 
                            "DriverFirstName": "Esri_Anonymous", 
                            "DriverLastName": "Aguilar", 
                            "LastUpdatedBy": "SANSTAR1", 
                            "Name": " ", 
                            "Type": "Dead Animal Removal"
                        }, 
                        {
                            "DACItemCount": "0", 
                            "DACType": " ", 
                            "DriverFirstName": "Esri_Anonymous", 
                            "DriverLastName": "Aguilar", 
                            "LastUpdatedBy": "SANSTAR1", 
                            "Name": " ", 
                            "Type": "Dead Animal Removal"
                        }, 
                        {
                            "DACItemCount": "0", 
                            "DACType": " ", 
                            "DriverFirstName": "Esri_Anonymous", 
                            "DriverLastName": "Aguilar", 
                            "LastUpdatedBy": "SANSTAR1", 
                            "Name": " ", 
                            "Type": "Dead Animal Removal"
                        }, 
                        {
                            "DACItemCount": "0", 
                            "DACType": " ", 
                            "DriverFirstName": "Esri_Anonymous", 
                            "DriverLastName": "Aguilar", 
                            "LastUpdatedBy": "SANSTAR1", 
                            "Name": " ", 
                            "Type": "Dead Animal Removal"
                        }, 
                        {
                            "DACItemCount": "0", 
                            "DACType": " ", 
                            "DriverFirstName": "Esri_Anonymous", 
                            "DriverLastName": "Aguilar", 
                            "LastUpdatedBy": "SANSTAR1", 
                            "Name": " ", 
                            "Type": "Dead Animal Removal"
                        }, 
                        {
                            "DACItemCount": "0", 
                            "DACType": " ", 
                            "DriverFirstName": "Esri_Anonymous", 
                            "DriverLastName": "Aguilar", 
                            "LastUpdatedBy": "SANSTAR1", 
                            "Name": " ", 
                            "Type": "Dead Animal Removal"
                        }
                    ]
                }, 
                "ReasonCode": "", 
                "ResolutionCode": "A", 
                "SRNumber": "1-20979921"
            }
        }
    ]
    [
        {
            "MetaData": {}, 
            "SRData": {
                "ListOfLa311DeadAnimalRemoval": {
                    "DeadAnimalRemoval": [
                        {
                            "DACItemCount": "0", 
                            "DACType": " ", 
                            "DriverFirstName": "Esri_Anonymous", 
                            "DriverLastName": "Aguilar", 
                            "LastUpdatedBy": "SANSTAR1", 
                            "Name": "070920151119458601", 
                            "Type": "Dead Animal Removal"
                        }, 
                        {
                            "DACItemCount": "0", 
                            "DACType": " ", 
                            "DriverFirstName": "Esri_Anonymous", 
                            "DriverLastName": "Aguilar", 
                            "LastUpdatedBy": "SANSTAR1", 
                            "Name": " ", 
                            "Type": "Dead Animal Removal"
                        }, 
                        {
                            "DACItemCount": "0", 
                            "DACType": " ", 
                            "DriverFirstName": "Esri_Anonymous", 
                            "DriverLastName": "Aguilar", 
                            "LastUpdatedBy": "SANSTAR1", 
                            "Name": " ", 
                            "Type": "Dead Animal Removal"
                        }, 
                        {
                            "DACItemCount": "0", 
                            "DACType": " ", 
                            "DriverFirstName": "Esri_Anonymous", 
                            "DriverLastName": "Aguilar", 
                            "LastUpdatedBy": "SANSTAR1", 
                            "Name": " ", 
                            "Type": "Dead Animal Removal"
                        }, 
                        {
                            "DACItemCount": "0", 
                            "DACType": " ", 
                            "DriverFirstName": "Esri_Anonymous", 
                            "DriverLastName": "Aguilar", 
                            "LastUpdatedBy": "SANSTAR1", 
                            "Name": " ", 
                            "Type": "Dead Animal Removal"
                        }, 
                        {
                            "DACItemCount": "0", 
                            "DACType": " ", 
                            "DriverFirstName": "Esri_Anonymous", 
                            "DriverLastName": "Aguilar", 
                            "LastUpdatedBy": "SANSTAR1", 
                            "Name": " ", 
                            "Type": "Dead Animal Removal"
                        }, 
                        {
                            "DACItemCount": "0", 
                            "DACType": " ", 
                            "DriverFirstName": "Esri_Anonymous", 
                            "DriverLastName": "Aguilar", 
                            "LastUpdatedBy": "SANSTAR1", 
                            "Name": " ", 
                            "Type": "Dead Animal Removal"
                        }, 
                        {
                            "DACItemCount": "0", 
                            "DACType": " ", 
                            "DriverFirstName": "Esri_Anonymous", 
                            "DriverLastName": "Aguilar", 
                            "LastUpdatedBy": "SANSTAR1", 
                            "Name": " ", 
                            "Type": "Dead Animal Removal"
                        }, 
                        {
                            "DACItemCount": "0", 
                            "DACType": " ", 
                            "DriverFirstName": "Esri_Anonymous", 
                            "DriverLastName": "Aguilar", 
                            "LastUpdatedBy": "SANSTAR1", 
                            "Name": " ", 
                            "Type": "Dead Animal Removal"
                        }, 
                        {
                            "DACItemCount": "0", 
                            "DACType": " ", 
                            "DriverFirstName": "Esri_Anonymous", 
                            "DriverLastName": "Aguilar", 
                            "LastUpdatedBy": "SANSTAR1", 
                            "Name": " ", 
                            "Type": "Dead Animal Removal"
                        }

带有空白名称字段的lResults的JSON

i = 0;

1 个答案:

答案 0 :(得分:0)

以下是解决问题的一种方法:完成构建l311列表后,浏览每个项目(字典)并根据您的条件删除密钥:

l311 = [
    dict(
        DriverFirstName=last_edited_user,
        DriverLastName='Aguilar',
        LastUpdatedBy='SANSTAR1',
        DACType=dar_item_1,
        Type='Dead Animal Removal',
        Name=dar_uid_1,
        DACItemCount=dar_qyt_1),
    dict(
        DriverFirstName=last_edited_user,
        DriverLastName='Aguilar',
        LastUpdatedBy='SANSTAR1',
        DACType=dar_item_2,
        Type='Dead Animal Removal',
        Name=dar_uid_2,
        DACItemCount=dar_qyt_2),
    dict(
        DriverFirstName=last_edited_user,
        DriverLastName='Aguilar',
        LastUpdatedBy='SANSTAR1',
        DACType=dar_item_3,
        Type='Dead Animal Removal',
        Name=dar_uid_3,
        DACItemCount=dar_qyt_3),
    # ...
]

for d in l311:
    if not d['Name'].strip() and d['DACItemCount'] < 0:
        del d['Name']
        del d['DACItemCount']