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

如何使用Jolt为数组所有元素添加固定前缀?

Jolt转换:为字符串数组元素添加固定前缀并提取指定部分

需求

为数组中所有元素先提取下划线前的内容,再为其添加固定前缀“0”。

输入输出示例

  • 输入JSON:
{"input": ["1_ANY","2_ANY","3_ANY"]}
  • 当前转换输出:
{"output" : ["1","2","3"]}
  • 期望输出:
{"output" : ["01","02","03"]}

已尝试的Jolt规则

[{"operation": "shift","spec": {"input": {"*": {"*_*": {"$(0,1)": "output"}}}}]

解决方案

可以通过两步Jolt操作实现需求:先提取目标内容,再拼接前缀。具体规则如下:

[
  {
    "operation": "shift",
    "spec": {
      "input": {
        "*": {
          "*_*": {
            "$(0,1)": "output[]"
          }
        }
      }
    }
  },
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "output": {
        "*": "=concat('0', @(1))"
      }
    }
  }
]

规则说明

  1. shift操作:遍历input数组的每个元素,通过*_*匹配下划线分隔的结构,用$(0,1)提取下划线前的内容,存入output数组;
  2. modify-overwrite-beta操作:调用concat函数,为output数组的每个元素拼接前缀“0”,覆盖原数组内容,得到最终期望格式。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.01 17:42:03