如何使用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))" } } } ]
规则说明
- shift操作:遍历
input数组的每个元素,通过*_*匹配下划线分隔的结构,用$(0,1)提取下划线前的内容,存入output数组; - modify-overwrite-beta操作:调用
concat函数,为output数组的每个元素拼接前缀“0”,覆盖原数组内容,得到最终期望格式。
内容的提问来源于stack exchange,提问作者VIshal Choudhary
相关产品推荐
相关产品推荐

