You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在JQ中使用多条件筛选匹配指定资源类型及操作

解决JQ多条件筛选命令报错问题

需要从给定的JSON数据中筛选出满足以下两个条件的条目:

  • type字段值为google_project_iam_binding
  • change.actions数组包含delete或update操作

你已成功写出单条件筛选命令:

jq '.resource_changes[] | select(any(.change.actions[]; .=="delete" or .=="update"))'

但尝试组合多条件时出现报错,错误命令如下:

jq '.resource_changes[] | select((.type == "google_project_iam_binding") and (.change.actions[]; .=="delete" or .=="update"))'

错误原因

第二个条件的写法不符合JQ语法:.change.actions[]; .=="delete" or .=="update"这种形式无法正确判断数组元素是否符合条件,必须使用any()函数遍历数组并检查是否存在匹配项。

正确命令

将两个条件用and连接,同时保留any()函数处理数组判断逻辑:

jq '.resource_changes[] | select(.type == "google_project_iam_binding" and any(.change.actions[]; . == "delete" or . == "update"))'

验证效果

用你提供的JSON数据测试该命令,会返回唯一符合条件的条目:

{
  "address": "module.serviceproject.module.project-cfg.google_project_iam_binding.roles[\"roles/cloudkms.admin\"]",
  "module_address": "module.serviceproject.module.project-cfg",
  "mode": "managed",
  "type": "google_project_iam_binding",
  "name": "roles",
  "index": "roles/cloudkms.admin",
  "provider_name": "registry.terraform.io/hashicorp/google",
  "change": {
    "actions": [
      "delete",
      "update"
    ]
  }
}

内容的提问来源于stack exchange,提问作者Antham

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.14 17:01:15