如何使用 JMESPath 根据子属性数组中包含的值过滤对象数组

问题描述

如何使用 JMESPath 过滤拥有准确电子邮件的电子邮件接收者的节点? 我有 JSON 对象:

   [     
     {
        "test":1,"emailReceivers": [
          {
            "emailAddress": "[email protected]",}
        ]
      },{
        "test":2,"emailReceivers": [
          {
            "emailAddress": "[email protected]",},{
            "emailAddress": "[email protected]",}
        ]
      }
    ]

我想通过包含 [email protected] 的 elememailReceivers 过滤后获取节点:

     {
        "test":1,}
        ]
      }

我尝试使用文档 https://jmespath.org/ 和示例,但失败了。

解决方法

您确实可以在过滤器上使用 contains 并询问包含特定值的子键。

随着查询

for row in table.findAll('tr'):
            try:
                col = row.findAll('td')
                team_ = col[2].text
                squad_ = col[3].text
                age_ = col[4].text
                foreigners_ = col[5].text
                total_ = col[6].text
                average_ = col[7].text
                team.append(team_)
                squad.append(squad_)
                age.append(age_)
                foreigners.append(foreigners_)
                total_market_value.append(total_)
                average_market_value.append(average_)
            except:
                team.append('')
                squad.append('')
                age.append('')
                foreigners.append('')
                total_market_value.append('')
                average_market_value.append('')

这将给出:

[?contains(emailReceivers[].emailAddress,'[email protected]')]

在您的示例数组上:

[
  {
    "test": 1,"emailReceivers": [
      {
        "emailAddress": "[email protected]"
      }
    ]
  }
]