如何在 Python 上过滤嵌套的 json 数组

How to filter nested json array on Python

我在 python 上有一个 json 数组,我想根据以下值进行过滤:my_json_array['models']['variants']['condition']['type']

我的 json 数组如下所示:

my_json_array = [
 {'id': 1,
 'models': [
   {'color': {'code': '#626262', 'name': 'Gray'},
   'variants': [{'id': 1,
     'condition': [{'id': 1,
       'type': 'type1',
        'value': 14900},
      {'id': 2,
       'type': 'type2',
        'value': 14000}]]
]
}]

我正在寻找一种方法来删除带有 type = type2 的条件项。输出应如下所示:

my_json_array = [{
 'id': 1,
 'models': [
   {'color': {'code': '#626262', 'name': 'Gray'},
   'variants': [{'id': 1,
     'condition': [{'id': 1,
       'type': 'type1',
        'value': 14900}]]
]
}]

你是这个意思吗?

my_json_array = [
  {
    'id': 1,
    'models': [
      {
        'color': {'code': '#626262', 'name': 'Gray'},
        'variants': [
          {
            'id': 1,
            'condition': [
              {
                'id': 1,
                'type': 'type1',
                'value': 14900
              },
              {
                'id': 2,
                'type': 'type2',
                'value': 14000
              }
            ]
          }
        ]
      }
    ]
  }
]
for mydict in my_json_array:
  for model in mydict['models']:
    for variant in model['variants']:
      for condition in variant['condition']:
        if condition['type']=="type2":
          variant['condition'].remove(condition)
print(my_json_array) # [{'id': 1, 'models': [{'color': {'code': '#626262', 'name': 'Gray'}, 'variants': [{'id': 1, 'condition': [{'id': 1, 'type': 'type1', 'value': 14900}]}]}]}]