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

如何编写JSON Path获取Condition.type为1对应的element值

兼容单个对象/数组的JSON Path筛选方案

给定如下JSON结构:

{
  "customer": [
    {
      "Addresses": {
        "addressDetails": {
          "element": "one"
        },
        "Condition": {
          "type": 1
        }
      }
    },
    {
      "Addresses": [
        {
          "addressDetails": {
            "element": "two"
          },
          "Condition": {
            "type": 0
          }
        },
        {
          "addressDetails": {
            "element": "three"
          },
          "Condition": {
            "type": 1
          }
        }
      ]
    }
  ]
}

其中Addresses字段既可能是单个对象,也可能是对象数组,需要提取所有Condition.type等于1的项对应的addressDetails.element值,目标结果为"one"和"three"。

可行的JSON Path写法

使用递归遍历+条件筛选的组合语法,同时兼容两种场景:

$..Addresses[?(@.Condition.type == 1)].addressDetails.element

语法说明

  • $..Addresses:递归遍历整个JSON结构,匹配所有名为Addresses的节点,不管它是单个对象还是数组
  • [?(@.Condition.type == 1)]:对匹配到的每个Addresses节点(包括数组内的单个元素),筛选出Condition.type等于1的项
  • .addressDetails.element:提取符合条件项的addressDetails.element值

用这个路径查询目标JSON,会直接返回["one", "three"],完全符合预期。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.24 23:09:55