从JSON中删除JSON对象?

2024-10-01 15:44:50 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个json数组作为

{
  "query": {
    "bool": {
      "must": [],
      "should": [
        {
          "match": {
            "Name": {
              "query": "Nametest",
              "fuzziness": 3,
              "boost": 5
            }
          }
        },
        {
          "match": {
            "Address": {
              "query": "NONE",
              "fuzziness": 3,
              "boost": 4
            }
          }
        },
        {
          "match": {
            "Site": {
              "query": "Adeswfvfv",
              "fuzziness": 3,
              "boost": 4
            }
          }
        },
        {
          "match": {
            "Phone": {
              "query": "5680728.00",
              "fuzziness": 2,
              "boost": 4
            }
          }
        }
      ],
      "minimum_should_match": 2
    }
  }
}

所以我想做的是,如果在json['query']['bool']['should']中,如果“query”是“NONE”,那么我想删除这个json数组,新的json将被删除

{
  "query": {
    "bool": {
      "must": [],
      "should": [
        {
          "match": {
            "Name": {
              "query": "Nametest",
              "fuzziness": 3,
              "boost": 5
            }
          }
        },
        {
          "match": {
            "Site": {
              "query": "Adeswfvfv",
              "fuzziness": 3,
              "boost": 4
            }
          }
        },
        {
          "match": {
            "Phone": {
              "query": "5680728.00",
              "fuzziness": 2,
              "boost": 4
            }
          }
        }
      ],
      "minimum_should_match": 2
    }
  }
}

我尝试过迭代json,并使用del(jsonarray)和pop(jsonarray),但没有任何帮助?你知道吗

尝试使用python json库,但失败

for e in q['query']['bool']['should']:
...     if "NONE" in str(e['match']):
...         del(e)

Tags: namenonejsonmatchsitephone数组query
2条回答

这应该会有帮助。你知道吗

import pprint 
d = {'query': {'bool': {'minimum_should_match': 2, 'should': [{'match': {'Name': {'query': 'Nametest', 'boost': 5, 'fuzziness': 3}}}, {'match': {'Address': {'query': 'NONE', 'boost': 4, 'fuzziness': 3}}}, {'match': {'Site': {'query': 'Adeswfvfv', 'boost': 4, 'fuzziness': 3}}}, {'match': {'Phone': {'query': '5680728.00', 'boost': 4, 'fuzziness': 2}}}], 'must': []}}}
d["query"]['bool']['should'] = [i for i in d["query"]['bool']['should'] if list(i['match'].items())[0][1]["query"] != 'NONE']
pprint.pprint(d)

输出:

{'query': {'bool': {'minimum_should_match': 2,
                    'must': [],
                    'should': [{'match': {'Name': {'boost': 5,
                                                   'fuzziness': 3,
                                                   'query': 'Nametest'}}},
                               {'match': {'Site': {'boost': 4,
                                                   'fuzziness': 3,
                                                   'query': 'Adeswfvfv'}}},
                               {'match': {'Phone': {'boost': 4,
                                                    'fuzziness': 2,
                                                    'query': '5680728.00'}}}]}}}

我写了这个,但这看起来很复杂

for p,c in  enumerate(json['query']['bool']['should']):
    if list(c["match"].values())[0]["query"] == "NONE":
        json['query']['bool']['should'].pop(p)
print(json)

相关问题 更多 >

    热门问题