首页 > 解决方案 > Python:迭代 JSON 并删除具有特定条件的项目

问题描述

我正在尝试使用 Python 从 API JSON 响应中过滤掉数据,但结果很奇怪。如果有人可以指导我如何处理这种情况,我会很高兴。

主要思想是删除 JSON 中的不相关数据,并仅保留与我在列表中保存的特定人员相关联的数据。这是 JSON 文件的片段:

{
  "result": [
    {
      "number": "Number1",
      "short_description": "Some Description",
      "assignment_group": {
        "display_value": "Some value",
        "link": "https://some_link.com"
      },
      "incident_state": "Closed",
      "sys_created_on": "2020-03-30 11:51:24",
      "priority": "4 - Low",
      "assigned_to": {
        "display_value": "John Doe",
        "link": "https://some_link.com"
      }
    },
    {
      "number": "Number2",
      "short_description": "Some Description",
      "assignment_group": {
        "display_value": "Some value",
        "link": "https://some_link.com"
      },
      "incident_state": "Closed",
      "sys_created_on": "2020-03-10 11:07:13",
      "priority": "4 - Low",
      "assigned_to": {
        "display_value": "Tyrell Greenley",
        "link": "https://some_link.com"
      }
    },
    {
      "number": "Number3",
      "short_description": "Some Description",
      "assignment_group": {
        "display_value": "Some value",
        "link": "https://some_link.com"
      },
      "incident_state": "Closed",
      "sys_created_on": "2020-03-20 10:23:35",
      "priority": "4 - Low",
      "assigned_to": {
        "display_value": "Delmar Vachon",
        "link": "https://some_link.com"
      }
    },
    {
      "number": "Number4",
      "short_description": "Some Description",
      "assignment_group": {
        "display_value": "Some value",
        "link": "https://some_link.com"
      },
      "incident_state": "Closed",
      "sys_created_on": "2020-03-30 11:51:24",
      "priority": "4 - Low",
      "assigned_to": {
        "display_value": "Samual Isham",
        "link": "https://some_link.com"
      }
    }
  ]
}

这是Python代码:

users_test = ['Ahmad Wickert', 'Dick Weston', 'Gerardo Salido', 'Rosendo Dewey', 'Samual Isham']

# Load JSON file
with open('extract.json', 'r') as input_file:
    input_data = json.load(input_file)


# Create a function to clear the data
def clear_data(data, users):
    """Filter out the data and leave only records for the names in the users_test list"""
    for elem in data:
        print(elem['assigned_to']['display_value'] not in users)
        if elem['assigned_to']['display_value'] not in users:
            print('Removing {} from JSON as not present in list of names.'.format(elem['assigned_to']['display_value']))
            data.remove(elem)
        else:
            print('Keeping the record for {} in JSON.'.format(elem['assigned_to']['display_value']))

    return data


cd = clear_data(input_data['result'], users_test)

这是输出,它似乎只遍历了文件中的两个项目:

True
Removing John Doe from JSON as not present in list of names.
True
Removing Delmar Vachon from JSON as not present in list of names.

Process finished with exit code 0

似乎问题或多或少与 .remove() 方法有关,但是我没有找到任何其他合适的解决方案来删除这些我不需要的特定项目。

这是没有应用 remove() 方法的迭代输出:

True
Removing John Doe from JSON as not present in list of names.
True
Removing Tyrell Greenley from JSON as not present in list of names.
True
Removing Delmar Vachon from JSON as not present in list of names.
False
Keeping the record for Samual Isham in JSON.

Process finished with exit code 0

注意:我故意留下了名称可见的支票。

我将不胜感激任何想法来解决这种情况。

标签: pythonjsonlist

解决方案


如果您不需要记录有关您要删除的人的信息,您可以简单地尝试

filtered = [i for i in data['result'] if i['assigned_to']['display_value'] in users_test]



推荐阅读