python 删除 json 中的所有特定键

python delete all specific keys in json

使用 Python 2.7 我想删除 JSON 中名称为 errCode 和 errMsg

的所有键

我的JSON例子:

json_string = '''\
{
    "vacation": 
    [
        {
            "dates": [
                {
                    "duration": 360, 
                    "dateTo": "4.2.2014", 
                    "dateFrom": "2.1.2014"
                }
            ], 
            "worker": "8"
        }, 
        {
            "dates": [
                {
                    "duration": 420, 
                    "dateTo": "", 
                    "dateFrom": "29.01.2015",
                    "errCode": "1", 
                    "errMsg": "Missing dateTo"
                }
            ], 
            "worker": "2"
        }
    ], 
    "general": {
        "scriptComment": "", 
        "scriptTo": "", 
        "errCode": "2", 
        "errMsg": "Missing comment.", 
        "scriptFrom": "01.01.2014"
    }
}
'''

所以我需要删除我 Json 中的所有 errMsgs 和 errCodes,无论级别如何

将您的 JSON 解码为 python 字典,然后递归删除键:

import json

def remove_error_info(d):
    if not isinstance(d, (dict, list)):
        return d
    if isinstance(d, list):
        return [remove_error_info(v) for v in d]
    return {k: remove_error_info(v) for k, v in d.items()
            if k not in {'errMsg', 'errCode'}}

data = json.loads(json_string)
data = remove_error_info(data)
json_string = json.dumps(data)

演示:

>>> import json
>>> json_string = '''\
... {
...     "vacation": 
...     [
...         {
...             "dates": [
...                 {
...                     "duration": 360, 
...                     "dateTo": "4.2.2014", 
...                     "dateFrom": "2.1.2014"
...                 }
...             ], 
...             "worker": "8"
...         }, 
...         {
...             "dates": [
...                 {
...                     "duration": 420, 
...                     "dateTo": "", 
...                     "dateFrom": "29.01.2015",
...                     "errCode": "1", 
...                     "errMsg": "Missing dateTo"
...                 }
...             ], 
...             "worker": "2"
...         }
...     ], 
...     "general": {
...         "scriptComment": "", 
...         "scriptTo": "", 
...         "errCode": "2", 
...         "errMsg": "Missing comment.", 
...         "scriptFrom": "01.01.2014"
...     }
... }
... '''
>>> def remove_error_info(d):
...     if not isinstance(d, (dict, list)):
...         return d
...     if isinstance(d, list):
...         return [remove_error_info(v) for v in d]
...     return {k: remove_error_info(v) for k, v in d.items()
...             if k not in {'errMsg', 'errCode'}}
... 
>>> data = json.loads(json_string)
>>> data = remove_error_info(data)
>>> print(json.dumps(data, sort_keys=True, indent=4))
{
    "general": {
        "scriptComment": "",
        "scriptFrom": "01.01.2014",
        "scriptTo": ""
    },
    "vacation": [
        {
            "dates": [
                {
                    "dateFrom": "2.1.2014",
                    "dateTo": "4.2.2014",
                    "duration": 360
                }
            ],
            "worker": "8"
        },
        {
            "dates": [
                {
                    "dateFrom": "29.01.2015",
                    "dateTo": "",
                    "duration": 420
                }
            ],
            "worker": "2"
        }
    ]
}