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

如何在Jolt中比较字符串并过滤JSON数据?

Jolt实现按字段值过滤JSON数据及字符串比较方法

需求说明

需要根据输入JSON中from_Store和to_Store字段的值过滤数据:

  • 若两个字段值不匹配,保留该完整元素
  • 若两个字段值匹配,移除该元素
    同时询问Jolt是否支持字符串比较函数。

输入JSON

[
  {
    "from_Store": "Test1",
    "to_Store": "Test1",
    "items": [
      {
        "UPC": "8240959370255",
        "shippedQuantity": 1
      }
    ]
  },
  {
    "from_Store": "Test2",
    "to_Store": "Test3",
    "items": [
      {
        "UPC": "8240959370210",
        "shippedQuantity": 1
      }
    ]
  }
]

预期输出JSON

[
  {
    "from_Store": "Test2",
    "to_Store": "Test3",
    "items": [
      {
        "UPC": "8240959370210",
        "shippedQuantity": 1
      }
    ]
  }
]

解决方案

Jolt支持字符串比较,可通过notEqual操作符实现该过滤需求,对应的Jolt规范如下:

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "from_Store": {
          "@(1,to_Store)": {
            "notEqual": {
              "$2": "[&3]"
            }
          }
        }
      }
    }
  }
]

规范说明:

  1. * 遍历输入数组中的每个元素
  2. @(1,to_Store) 引用当前元素下的to_Store字段值,与当前from_Store的值进行比较
  3. notEqual 判断两个值是否不相等,若满足条件,通过$2引用整个当前元素(即&3对应的数组元素位置),将其保留到输出数组中

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.16 23:13:20