删除某些键具有空值的JSON对象

时间:2018-03-19 15:24:26

标签: python json

我有一个json如下:

[{
        "Domain": "google.com",
        "A": ["172.217.22.46"],
        "AAAA": ["2a00:1450:4001:81e::200e"],
        "CAA": ["0 issue \"pki.goog\""],
        "MX": ["20 alt1.aspmx.l.google.com.", "30 alt2.aspmx.l.google.com.", "10 aspmx.l.google.com.", "40 alt3.aspmx.l.google.com.", "50 alt4.aspmx.l.google.com."],
        "NS": ["ns1.google.com.", "ns3.google.com.", "ns2.google.com.", "ns4.google.com."],
        "SOA": ["ns1.google.com. dns-admin.google.com. 189483475 900 900 1800 60"],
        "TXT": ["\"docusign=05958488-4752-4ef2-95eb-aa7ba8a3bd0e\"", "\"v=spf1 include:_spf.google.com ~all\""],
        "Country": ["United States"],
        "Hostname": ["'fra15s16-in-f46.1e100.net'"],
        "SSL": ["Google Internet Authority G2"],
        "WHOIS": [8400],
        "TTL": ["24"]
    }, {
        "Domain": "",
        "NS": ["a.root-servers.net.", "h.root-servers.net.", "g.root-servers.net.", "i.root-servers.net.", "d.root-servers.net.", "k.root-servers.net.", "c.root-servers.net.", "l.root-servers.net.", "e.root-servers.net.", "m.root-servers.net.", "f.root-servers.net.", "j.root-servers.net.", "b.root-servers.net."],
        "SOA": ["a.root-servers.net. nstld.verisign-grs.com. 2018031700 1800 900 604800 86400"],
        "A": [],
        "SSL": ["None"],
        "WHOIS": [0],
        "TTL": [null]
    }, {
        "Domain": "facebook.com",
        "A": ["157.240.20.35"],
        "AAAA": ["2a03:2880:f11c:8183:face:b00c:0:25de"],
        "MX": ["10 msgin.vvv.facebook.com."],
        "NS": ["a.ns.facebook.com.", "b.ns.facebook.com."],
        "SOA": ["a.ns.facebook.com. dns.facebook.com. 1521364737 14400 1800 604800 300"],
        "TXT": ["\"v=spf1 redirect=_spf.facebook.com\""],
        "Country": ["United States"],
        "Hostname": ["'edge-star-mini-shv-02-frt3.facebook.com'"],
        "SSL": ["DigiCert SHA2 High Assurance Server CA"],
        "WHOIS": [10227],
        "TTL": ["173"]
    }]

请注意第二个元素,即"Domain" : "",我要删除的元素(可能有多个"Domain" : ""就是这样的实例,这只是部分JSON)并返回一个有效的json缺少整个对象,所以输出必须如下:

[{
        "Domain": "google.com",
        "A": ["172.217.22.46"],
        "AAAA": ["2a00:1450:4001:81e::200e"],
        "CAA": ["0 issue \"pki.goog\""],
        "MX": ["20 alt1.aspmx.l.google.com.", "30 alt2.aspmx.l.google.com.", "10 aspmx.l.google.com.", "40 alt3.aspmx.l.google.com.", "50 alt4.aspmx.l.google.com."],
        "NS": ["ns1.google.com.", "ns3.google.com.", "ns2.google.com.", "ns4.google.com."],
        "SOA": ["ns1.google.com. dns-admin.google.com. 189483475 900 900 1800 60"],
        "TXT": ["\"docusign=05958488-4752-4ef2-95eb-aa7ba8a3bd0e\"", "\"v=spf1 include:_spf.google.com ~all\""],
        "Country": ["United States"],
        "Hostname": ["'fra15s16-in-f46.1e100.net'"],
        "SSL": ["Google Internet Authority G2"],
        "WHOIS": [8400],
        "TTL": ["24"]
    }, {
        "Domain": "facebook.com",
        "A": ["157.240.20.35"],
        "AAAA": ["2a03:2880:f11c:8183:face:b00c:0:25de"],
        "MX": ["10 msgin.vvv.facebook.com."],
        "NS": ["a.ns.facebook.com.", "b.ns.facebook.com."],
        "SOA": ["a.ns.facebook.com. dns.facebook.com. 1521364737 14400 1800 604800 300"],
        "TXT": ["\"v=spf1 redirect=_spf.facebook.com\""],
        "Country": ["United States"],
        "Hostname": ["'edge-star-mini-shv-02-frt3.facebook.com'"],
        "SSL": ["DigiCert SHA2 High Assurance Server CA"],
        "WHOIS": [10227],
        "TTL": ["173"]
}]

这是我尝试的(示例可执行):

import json

if __name__ == "__main__":

    json_data = "[{\r\n\t\t\"Domain\": \"google.com\",\r\n\t\t\"A\": [\"172.217.22.46\"],\r\n\t\t\"AAAA\": [\"2a00:1450:4001:81e::200e\"],\r\n\t\t\"CAA\": [\"0 issue \\\"pki.goog\\\"\"],\r\n\t\t\"MX\": [\"20 alt1.aspmx.l.google.com.\", \"30 alt2.aspmx.l.google.com.\", \"10 aspmx.l.google.com.\", \"40 alt3.aspmx.l.google.com.\", \"50 alt4.aspmx.l.google.com.\"],\r\n\t\t\"NS\": [\"ns1.google.com.\", \"ns3.google.com.\", \"ns2.google.com.\", \"ns4.google.com.\"],\r\n\t\t\"SOA\": [\"ns1.google.com. dns-admin.google.com. 189483475 900 900 1800 60\"],\r\n\t\t\"TXT\": [\"\\\"docusign=05958488-4752-4ef2-95eb-aa7ba8a3bd0e\\\"\", \"\\\"v=spf1 include:_spf.google.com ~all\\\"\"],\r\n\t\t\"Country\": [\"United States\"],\r\n\t\t\"Hostname\": [\"'fra15s16-in-f46.1e100.net'\"],\r\n\t\t\"SSL\": [\"Google Internet Authority G2\"],\r\n\t\t\"WHOIS\": [8400],\r\n\t\t\"TTL\": [\"24\"]\r\n\t}, {\r\n\t\t\"Domain\": \"\",\r\n\t\t\"NS\": [\"a.root-servers.net.\", \"h.root-servers.net.\", \"g.root-servers.net.\", \"i.root-servers.net.\", \"d.root-servers.net.\", \"k.root-servers.net.\", \"c.root-servers.net.\", \"l.root-servers.net.\", \"e.root-servers.net.\", \"m.root-servers.net.\", \"f.root-servers.net.\", \"j.root-servers.net.\", \"b.root-servers.net.\"],\r\n\t\t\"SOA\": [\"a.root-servers.net. nstld.verisign-grs.com. 2018031700 1800 900 604800 86400\"],\r\n\t\t\"A\": [],\r\n\t\t\"SSL\": [\"None\"],\r\n\t\t\"WHOIS\": [0],\r\n\t\t\"TTL\": [null]\r\n\t}, {\r\n\t\t\"Domain\": \"facebook.com\",\r\n\t\t\"A\": [\"157.240.20.35\"],\r\n\t\t\"AAAA\": [\"2a03:2880:f11c:8183:face:b00c:0:25de\"],\r\n\t\t\"MX\": [\"10 msgin.vvv.facebook.com.\"],\r\n\t\t\"NS\": [\"a.ns.facebook.com.\", \"b.ns.facebook.com.\"],\r\n\t\t\"SOA\": [\"a.ns.facebook.com. dns.facebook.com. 1521364737 14400 1800 604800 300\"],\r\n\t\t\"TXT\": [\"\\\"v=spf1 redirect=_spf.facebook.com\\\"\"],\r\n\t\t\"Country\": [\"United States\"],\r\n\t\t\"Hostname\": [\"'edge-star-mini-shv-02-frt3.facebook.com'\"],\r\n\t\t\"SSL\": [\"DigiCert SHA2 High Assurance Server CA\"],\r\n\t\t\"WHOIS\": [10227],\r\n\t\t\"TTL\": [\"173\"]\r\n\t}]"
    ds = json.loads(json_data)
    # cleaning empty entries
    for item in ds:
        try:
            if item["Domain"]:
                del item["Domain"]
        except KeyError:
            print("Key doesn't exist")
    print(ds)

然而,我无法实现目标。怎么做到这一点? 谢谢!

5 个答案:

答案 0 :(得分:3)

[dom for dom in ds if dom.get('Domain')]

使用列表推导来选择所有对“域”具有非假值的字典。

答案 1 :(得分:2)

通过有选择地选择所需元素来 创建新对象 的过程比删除元素和修改对象更好,更不容易出错。

由于您已完成json部分,因此我们将向您展示如何获取所需的dict。它很简单:

>>> [ele for ele in d if ele['Domain']]

其中d是从输入json获得的字典。

#driver values:

IN : d = [{'Domain':'A', 'xyz':'abc'}, {'Domain':'', 'xyz':'bcd'},
          {'Domain':'C', 'xyz':'cde'}]

OUT : [{'Domain': 'A', 'xyz': 'abc'}, {'Domain': 'C', 'xyz': 'cde'}]

答案 2 :(得分:1)

您的问题是您正在删除对象中的键而不是整个对象。

import json

if __name__ == "__main__":

    json_data = "[{\r\n\t\t\"Domain\": \"google.com\",\r\n\t\t\"A\": [\"172.217.22.46\"],\r\n\t\t\"AAAA\": [\"2a00:1450:4001:81e::200e\"],\r\n\t\t\"CAA\": [\"0 issue \\\"pki.goog\\\"\"],\r\n\t\t\"MX\": [\"20 alt1.aspmx.l.google.com.\", \"30 alt2.aspmx.l.google.com.\", \"10 aspmx.l.google.com.\", \"40 alt3.aspmx.l.google.com.\", \"50 alt4.aspmx.l.google.com.\"],\r\n\t\t\"NS\": [\"ns1.google.com.\", \"ns3.google.com.\", \"ns2.google.com.\", \"ns4.google.com.\"],\r\n\t\t\"SOA\": [\"ns1.google.com. dns-admin.google.com. 189483475 900 900 1800 60\"],\r\n\t\t\"TXT\": [\"\\\"docusign=05958488-4752-4ef2-95eb-aa7ba8a3bd0e\\\"\", \"\\\"v=spf1 include:_spf.google.com ~all\\\"\"],\r\n\t\t\"Country\": [\"United States\"],\r\n\t\t\"Hostname\": [\"'fra15s16-in-f46.1e100.net'\"],\r\n\t\t\"SSL\": [\"Google Internet Authority G2\"],\r\n\t\t\"WHOIS\": [8400],\r\n\t\t\"TTL\": [\"24\"]\r\n\t}, {\r\n\t\t\"Domain\": \"\",\r\n\t\t\"NS\": [\"a.root-servers.net.\", \"h.root-servers.net.\", \"g.root-servers.net.\", \"i.root-servers.net.\", \"d.root-servers.net.\", \"k.root-servers.net.\", \"c.root-servers.net.\", \"l.root-servers.net.\", \"e.root-servers.net.\", \"m.root-servers.net.\", \"f.root-servers.net.\", \"j.root-servers.net.\", \"b.root-servers.net.\"],\r\n\t\t\"SOA\": [\"a.root-servers.net. nstld.verisign-grs.com. 2018031700 1800 900 604800 86400\"],\r\n\t\t\"A\": [],\r\n\t\t\"SSL\": [\"None\"],\r\n\t\t\"WHOIS\": [0],\r\n\t\t\"TTL\": [null]\r\n\t}, {\r\n\t\t\"Domain\": \"facebook.com\",\r\n\t\t\"A\": [\"157.240.20.35\"],\r\n\t\t\"AAAA\": [\"2a03:2880:f11c:8183:face:b00c:0:25de\"],\r\n\t\t\"MX\": [\"10 msgin.vvv.facebook.com.\"],\r\n\t\t\"NS\": [\"a.ns.facebook.com.\", \"b.ns.facebook.com.\"],\r\n\t\t\"SOA\": [\"a.ns.facebook.com. dns.facebook.com. 1521364737 14400 1800 604800 300\"],\r\n\t\t\"TXT\": [\"\\\"v=spf1 redirect=_spf.facebook.com\\\"\"],\r\n\t\t\"Country\": [\"United States\"],\r\n\t\t\"Hostname\": [\"'edge-star-mini-shv-02-frt3.facebook.com'\"],\r\n\t\t\"SSL\": [\"DigiCert SHA2 High Assurance Server CA\"],\r\n\t\t\"WHOIS\": [10227],\r\n\t\t\"TTL\": [\"173\"]\r\n\t}]"
    ds = [d 
          for d in json.loads(json_data)
          if d.get("Domain")
         ]

答案 3 :(得分:1)

试试这个:

import json

if __name__ == "__main__":

    json_data = "[{\r\n\t\t\"Domain\": \"google.com\",\r\n\t\t\"A\": [\"172.217.22.46\"],\r\n\t\t\"AAAA\": [\"2a00:1450:4001:81e::200e\"],\r\n\t\t\"CAA\": [\"0 issue \\\"pki.goog\\\"\"],\r\n\t\t\"MX\": [\"20 alt1.aspmx.l.google.com.\", \"30 alt2.aspmx.l.google.com.\", \"10 aspmx.l.google.com.\", \"40 alt3.aspmx.l.google.com.\", \"50 alt4.aspmx.l.google.com.\"],\r\n\t\t\"NS\": [\"ns1.google.com.\", \"ns3.google.com.\", \"ns2.google.com.\", \"ns4.google.com.\"],\r\n\t\t\"SOA\": [\"ns1.google.com. dns-admin.google.com. 189483475 900 900 1800 60\"],\r\n\t\t\"TXT\": [\"\\\"docusign=05958488-4752-4ef2-95eb-aa7ba8a3bd0e\\\"\", \"\\\"v=spf1 include:_spf.google.com ~all\\\"\"],\r\n\t\t\"Country\": [\"United States\"],\r\n\t\t\"Hostname\": [\"'fra15s16-in-f46.1e100.net'\"],\r\n\t\t\"SSL\": [\"Google Internet Authority G2\"],\r\n\t\t\"WHOIS\": [8400],\r\n\t\t\"TTL\": [\"24\"]\r\n\t}, {\r\n\t\t\"Domain\": \"\",\r\n\t\t\"NS\": [\"a.root-servers.net.\", \"h.root-servers.net.\", \"g.root-servers.net.\", \"i.root-servers.net.\", \"d.root-servers.net.\", \"k.root-servers.net.\", \"c.root-servers.net.\", \"l.root-servers.net.\", \"e.root-servers.net.\", \"m.root-servers.net.\", \"f.root-servers.net.\", \"j.root-servers.net.\", \"b.root-servers.net.\"],\r\n\t\t\"SOA\": [\"a.root-servers.net. nstld.verisign-grs.com. 2018031700 1800 900 604800 86400\"],\r\n\t\t\"A\": [],\r\n\t\t\"SSL\": [\"None\"],\r\n\t\t\"WHOIS\": [0],\r\n\t\t\"TTL\": [null]\r\n\t}, {\r\n\t\t\"Domain\": \"facebook.com\",\r\n\t\t\"A\": [\"157.240.20.35\"],\r\n\t\t\"AAAA\": [\"2a03:2880:f11c:8183:face:b00c:0:25de\"],\r\n\t\t\"MX\": [\"10 msgin.vvv.facebook.com.\"],\r\n\t\t\"NS\": [\"a.ns.facebook.com.\", \"b.ns.facebook.com.\"],\r\n\t\t\"SOA\": [\"a.ns.facebook.com. dns.facebook.com. 1521364737 14400 1800 604800 300\"],\r\n\t\t\"TXT\": [\"\\\"v=spf1 redirect=_spf.facebook.com\\\"\"],\r\n\t\t\"Country\": [\"United States\"],\r\n\t\t\"Hostname\": [\"'edge-star-mini-shv-02-frt3.facebook.com'\"],\r\n\t\t\"SSL\": [\"DigiCert SHA2 High Assurance Server CA\"],\r\n\t\t\"WHOIS\": [10227],\r\n\t\t\"TTL\": [\"173\"]\r\n\t}]"
    ds = json.loads(json_data)
    keep = []
    for item in ds:
        if item.get("Domain"):
            keep.append(item)
    print(json.dumps(keep))

此代码构建了要保留的项目的 new 列表。

答案 4 :(得分:1)

尝试以下方法:

for k in ds:
    if not ds[k]:
        ds.remove(k)

这样,如果k的值为dsk0,您就会从None移除False ,空列表或空字符串,它适用于任何给定的键。