检查JSON是否存在密钥

时间:2019-03-05 08:40:33

标签: python python-3.x

我正在遍历JSON以检查是否存在Key。我有意手动删除了所有不存在密钥的东西。

我的示例JSON是:

    {
    "MODELER": {
        "@version": "3",
        "MODEL": {
            "@BEGINDATE": "1970-01-01 00:00:00.0",
            "@DESCRIPTION": "SendPro C 200/300/400 (CSD 2.0) t2019.02.08 u2019.02.12",
            "@ENDDATE": "2117-05-03 08:24:36.0",
            "@ID": "I_604356",
            "@NAME": "SendProCSeries",
            "@PATH": "PB.SMB.SendProCSeries",
            "@SKU": "",
            "@SUBASSEMBLY_TYPE": "MODEL",
            "RULE": [
                {
                    "@COMMENT": "",
                    "@DEFAULTPRIORITY": "50",
                    "@DESCRIPTION": "Pick Meter Equipment",
                    "@DISABLEFLG": "N",
                    "@DefinedOutsideTree": "true",
                    "@FIRINGPHASE": "",
                    "@NAME": "PickMeterEquipment",
                    "BOOLOP": {
                        "@BOOLOP": "and",
                        "@SEQ": "0",
                        "@TYPE": "0",
                        "BOOLOP": [
                            {
                                "@BOOLOP": "or",
                                "@SEQ": "1",
                                "@TYPE": "0",
                                "FRAGMENT": [
                                    {
                                        "@FUNC1": "value",
                                        "@FUNC2": "literal",
                                        "@NULLACTION": "Fragment is false",
                                        "@OP": "=",
                                        "@PROP1": "SALES_TYPE",
                                        "@PROP2": "PURCHASE",
                                        "@SEQ": "1",
                                        "@TYPE": "1"
                                    },
                                    {
                                        "@FUNC1": "value",
                                        "@FUNC2": "literal",
                                        "@NULLACTION": "Fragment is false",
                                        "@OP": "=",
                                        "@PROP1": "SALES_TYPE",
                                        "@PROP2": "LEASE",
                                        "@SEQ": "2",
                                        "@TYPE": "1"
                                    }
                                ]
                            },
                            {
                                "@BOOLOP": "or",
                                "@SEQ": "2",
                                "@TYPE": "0",
                                "FRAGMENT": {
                                    "@FUNC1": "value",
                                    "@FUNC2": "list",
                                    "@NULLACTION": "Fragment is false",
                                    "@OP": "in",
                                    "@PROP1": "PRICELIST",
                                    "@PROP2": "LeaseAccounting",
                                    "@SEQ": "1",
                                    "@TYPE": "1"
                                }
                            },
                            {
                                "@BOOLOP": "or",
                                "@SEQ": "4",
                                "@TYPE": "0",
                                "FRAGMENT": [
                                    {
                                        "@FUNC1": "value",
                                        "@FUNC2": "propval",
                                        "@NULLACTION": "Fragment is false",
                                        "@OP": "=",
                                        "@PROP1": "PICK_SKU1",
                                        "@PROP2": "._sku",
                                        "@SEQ": "1",
                                        "@TYPE": "1"
                                    },
                                    {
                                        "@FUNC1": "value",
                                        "@FUNC2": "propval",
                                        "@NULLACTION": "Fragment is false",
                                        "@OP": "=",
                                        "@PROP1": "PICK_SKU10",
                                        "@PROP2": "._sku",
                                        "@SEQ": "2",
                                        "@TYPE": "1"
                                    }
                                ]
                            }
                        ]
                    },
                    "ACTION": [
                        {
                            "@FORMULA": "1",
                            "@PROPNAME": "",
                            "@PROPVALUE": "",
                            "@SEQ": "0",
                            "@TYPE": "1",
                            "LOCALES": null,
                            "ACTIONITEM": {
                                "@SEQ": "0",
                                "@item": ".",
                                "@max": "1",
                                "@min": "0",
                                "@qty": "1"
                            }
                        },
                        {
                            "@FORMULA": "",
                            "@PROPNAME": "_isViewable",
                            "@PROPVALUE": "1",
                            "@SEQ": "0",
                            "@TYPE": "2",
                            "LOCALES": null
                        }
                    ]
                },
                {
                    "@COMMENT": "",
                    "@DEFAULTPRIORITY": "50",
                    "@DESCRIPTION": "Pick If External Selected",
                    "@DISABLEFLG": "N",
                    "@DefinedOutsideTree": "true",
                    "@FIRINGPHASE": "",
                    "@NAME": "PickIfExternalScaleSelected",
                    "@TRIGGER": "1",
                    "BOOLOP": {
                        "@BOOLOP": "and",
                        "@SEQ": "0",
                        "@TYPE": "0",
                        "FRAGMENT": [
                            {
                                "@FUNC1": "length",
                                "@FUNC2": "literal",
                                "@NULLACTION": "Fragment is false",
                                "@OP": ">",
                                "@PROP1": "ExternalScaleVal",
                                "@PROP2": "0",
                                "@SEQ": "1",
                                "@TYPE": "1"
                            },
                            {
                                "@FUNC1": "value",
                                "@FUNC2": "literal",
                                "@NULLACTION": "Fragment is false",
                                "@OP": "=",
                                "@PROP1": "_amEntitled",
                                "@PROP2": "1",
                                "@SEQ": "2",
                                "@TYPE": "1"
                            }
                        ]
                    },
                    "ACTION": {
                        "@FORMULA": "1",
                        "@PROPNAME": "",
                        "@PROPVALUE": "",
                        "@SEQ": "0",
                        "@TYPE": "1",
                        "LOCALES": null,
                        "ACTIONITEM": {
                            "@SEQ": "0",
                            "@item": ".",
                            "@max": "1",
                            "@min": "0",
                            "@qty": "1"
                        }
                    }
                },
                {
                    "@COMMENT": "",
                    "@DEFAULTPRIORITY": "50",
                    "@NAME": "PickCAAA",
                    "BOOLOP": {
                        "@BOOLOP": "and",
                        "@SEQ": "0",
                        "@TYPE": "0",
                        "FRAGMENT": {
                            "@FUNC1": "value",
                            "@FUNC2": "literal",
                            "@NULLACTION": "Fragment is false",
                            "@OP": "=",
                            "@PROP1": "_description",
                            "@PROP2": "Dashboard",
                            "@SEQ": "1",
                            "@TYPE": "1"
                        }
                    },
                    "ACTION": {
                        "@FORMULA": "1",
                        "@PROPNAME": "",
                        "@PROPVALUE": "",
                        "@SEQ": "0",
                        "@TYPE": "1",
                        "LOCALES": null,
                        "ACTIONITEM": {
                            "@SEQ": "0",
                            "@item": ".",
                            "@max": "1",
                            "@min": "0",
                            "@qty": "1"
                        }
                    }
                },
                {
                    "@COMMENT": "",
                    "@DEFAULTPRIORITY": "50",
                    "@DESCRIPTION": "Pick ERB4 when ZHD8",
                    "@DISABLEFLG": "N",
                    "@FIRINGPHASE": "",
                    "@NAME": "AUTOSELECT: ERB4",
                    "@RULECLASSIFICATION": "PICK",
                    "@TRIGGER": "1",
                    "BOOLOP": {
                        "@BOOLOP": "and",
                        "@SEQ": "0",
                        "@TYPE": "0",
                        "FRAGMENT": {
                            "@FUNC1": "value",
                            "@FUNC2": "literal",
                            "@NULLACTION": "Fragment is false",
                            "@OP": "=",
                            "@PROP1": "_sku",
                            "@PROP2": "ZHD8",
                            "@SEQ": "1",
                            "@TYPE": "1"
                        }
                    },
                    "ACTION": [
                        {
                            "@FORMULA": "1",
                            "@PROPNAME": "",
                            "@PROPVALUE": "",
                            "@SEQ": "0",
                            "@TYPE": "1",
                            "LOCALES": null,
                            "ACTIONITEM": {
                                "@SEQ": "0",
                                "@item": ".",
                                "@max": "1",
                                "@min": "0",
                                "@qty": "1"
                            }
                        },
                        {
                            "@FORMULA": "",
                            "@PROPNAME": "_isViewable",
                            "@PROPVALUE": "1",
                            "@SEQ": "0",
                            "@TYPE": "2",
                            "LOCALES": null
                        }
                    ]
                }
            ]
        }
    }
}

现在,我要检查JSON中是否存在特定的子键。为此,我编写了一个函数,该函数应根据子键条件打印一个父键。

函数如下:

def getTargetPickRules(json_rule_data):
    for i in json_rule_data['MODELER']['MODEL']['RULE']:
        if 'ACTION' not in i:
            continue

        if 'ACTIONITEM' not in i['ACTION']:
            continue

        print("Rule Name - %s %s" % (i['@NAME'],i['ACTION']))

理想情况下,预期输出应为:

Rule Name - PickMeterEquipment [{'@FORMULA': '1', '@PROPNAME': '', '@PROPVALUE': '', '@SEQ': '0', '@TYPE': '1', 'LOCALES': None, 'ACTIONITEM': {'@SEQ': '0', '@item': '.', '@max': '1', '@min': '0', '@qty': '1'}}, {'@FORMULA': '', '@PROPNAME': '_isViewable', '@PROPVALUE': '1', '@SEQ': '0', '@TYPE': '2', 'LOCALES': None}]
Rule Name - PickIfExternalScaleSelected {'@FORMULA': '1', '@PROPNAME': '', '@PROPVALUE': '', '@SEQ': '0', '@TYPE': '1', 'LOCALES': None, 'ACTIONITEM': {'@SEQ': '0', '@item': '.', '@max': '1', '@min': '0', '@qty': '1'}}
Rule Name - PickCAAA {'@FORMULA': '1', '@PROPNAME': '', '@PROPVALUE': '', '@SEQ': '0', '@TYPE': '1', 'LOCALES': None, 'ACTIONITEM': {'@SEQ': '0', '@item': '.', '@max': '1', '@min': '0', '@qty': '1'}}
Rule Name - AUTOSELECT: ERB4 [{'@FORMULA': '1', '@PROPNAME': '', '@PROPVALUE': '', '@SEQ': '0', '@TYPE': '1', 'LOCALES': None, 'ACTIONITEM': {'@SEQ': '0', '@item': '.', '@max': '1', '@min': '0', '@qty': '1'}}, {'@FORMULA': '', '@PROPNAME': '_isViewable', '@PROPVALUE': '1', '@SEQ': '0', '@TYPE': '2', 'LOCALES': None}]

如果您看到我的JSON,则所有ACTION密钥都有ACTIONITEM,但仅输出其中两个。这是我的实际输出:

Rule Name - PickIfExternalScaleSelected {'@FORMULA': '1', '@PROPNAME': '', '@PROPVALUE': '', '@SEQ': '0', '@TYPE': '1', 'LOCALES': None, 'ACTIONITEM': {'@SEQ': '0', '@item': '.', '@max': '1', '@min': '0', '@qty': '1'}}
Rule Name - PickCAAA {'@FORMULA': '1', '@PROPNAME': '', '@PROPVALUE': '', '@SEQ': '0', '@TYPE': '1', 'LOCALES': None, 'ACTIONITEM': {'@SEQ': '0', '@item': '.', '@max': '1', '@min': '0', '@qty': '1'}}

2 个答案:

答案 0 :(得分:1)

您的数据不一致: 一些操作词典包含一个字典(该词典包含元素"ACTIONITEM"),而另一些包含列表(以[开头)。要么数据有误,要么您必须更精确地指定所需的行为。

答案 1 :(得分:0)

一个可能的问题是,某些条目中有多个ACTION。当您使用json模块将json加载到字典中时,在重复键的情况下,库的默认行为是保留重复键的最新值。这是因为python中的字典不能有重复的键。在您的情况下,JSON中更高版本的ACTION版本没有ACTIONITEM。

示例:

>>> import json
>>> dic = json.loads('{"a": "b", "a": "c"}')
>>> dic
{u'a': u'c'}
>>> dic = json.loads('{"a": "c", "a": "b"}')
{u'a': u'b'}