如何基于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": "" } } ]
规则解释
- 第一步shift操作:保留
mapTrue字段,同时将每个code元素与对应的expired值绑定,存入temp对象中——temp的键为expired的取值,值为对应code元素的数组。 - 第二步shift操作:通过
${mapTrue}引用mapTrue的实际取值,从temp中取出对应键的数组,赋值给最终的code字段。 - 第三步remove操作:移除
mapTrue字段,仅保留处理后的code数组。
测试验证
- 输入
mapTrue: "true"时,输出结果:{"code": ["X", "Z"]} - 输入
mapTrue: "false"时,输出结果:{"code": ["Y"]}
内容的提问来源于stack exchange,提问作者Eusebius
相关产品推荐
相关产品推荐

