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

如何基于mapTrue值使用Jolt Transform过滤code数组?

Jolt Transform 规则修改方案

原始输入JSON

{"mapTrue": "true","code": ["X","Y","Z"],"expired": ["true","false","true"]}

需求说明

  • 当mapTrue为"true"时,筛选expired中值为"true"对应的code元素,输出{"code": ["X", "Z"]}
  • 当mapTrue为"false"时,筛选expired中值为"false"对应的code元素,输出{"code": ["Y"]}

修改后的Jolt规则

[
  {
    "operation": "shift",
    "spec": {
      "mapTrue": "&",
      "code": {
        "*": {
          "@(2,expired[&1])": "temp.&.&1",
          "@": "temp.&.&1"
        }
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "temp": {
        "${mapTrue}": {
          "*": {
            "@": "code"
          }
        }
      }
    }
  },
  {
    "operation": "remove",
    "spec": {
      "mapTrue": ""
    }
  }
]

规则解释

  1. 第一步shift操作:保留mapTrue字段,同时将每个code元素与对应的expired值绑定,存入temp对象中——temp的键为expired的取值,值为对应code元素的数组。
  2. 第二步shift操作:通过${mapTrue}引用mapTrue的实际取值,从temp中取出对应键的数组,赋值给最终的code字段。
  3. 第三步remove操作:移除mapTrue字段,仅保留处理后的code数组。

测试验证

  • 输入mapTrue: "true"时,输出结果:{"code": ["X", "Z"]}
  • 输入mapTrue: "false"时,输出结果:{"code": ["Y"]}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.15 10:15:36