JOLT转换规则调整:处理空字符串场景下的字段填充需求
JOLT转换:处理secondAccount空字符串场景的解决方案
转换规则
- 若
secondAccount存在有效值,则保留该值 - 若
secondAccount为null、空字符串或未传入,则将其设为与firstAccount相同的值
当前问题
原JOLT Spec使用modify-default-beta操作,能处理null和未传入的场景,但无法处理secondAccount为空字符串的情况——空字符串会被保留,不会替换为firstAccount的值。
解决方案
新增一步操作,先将空字符串的secondAccount替换为null,再用原有的modify-default-beta逻辑填充值。完整JOLT Spec如下:
[ { "operation": "modify-overwrite-beta", "spec": { "data": { "*": { "accounts": { "secondAccount": ["=isNull", "", "@(1,secondAccount)"] } } } } }, { "operation": "modify-default-beta", "spec": { "data": { "*": { "accounts": { "secondAccount": "@(1,firstAccount)" } } } } } ]
逻辑说明
- 第一步(modify-overwrite-beta):使用JOLT的条件判断
=isNull,当secondAccount是空字符串时,将其设为null;如果是有效值则保持不变。 - 第二步(modify-default-beta):对
null或未传入的secondAccount,填充为firstAccount的值,同时保留有效值。
测试验证
对示例输入执行上述Spec后,输出如下:
{ "data": [ { "accounts": { "firstAccount": "info1", "secondAccount": "info1" } }, { "accounts": { "firstAccount": "info1", "secondAccount": "info2" } }, { "accounts": { "firstAccount": "info1", "secondAccount": "info1" } }, { "accounts": { "firstAccount": "info1", "secondAccount": "info1" } }, { "accounts": { "firstAccount": "info1", "secondAccount": "info1" } } ] }
可以看到,原输入中secondAccount为空字符串的条目已被正确替换为firstAccount的值,同时所有有效值均被保留。
内容的提问来源于stack exchange,提问作者safrinho
相关产品推荐
相关产品推荐

