如何在Jolt中添加Null值校验?场景配置问题求助
解决Jolt映射中null值的默认值设置问题
问题分析
你当前的Spec里,shift操作中的"null"是用来匹配键名为null的节点,而非值为null的phone字段,所以场景2的null校验逻辑无法生效。要实现phone为null时给customer.mobile设置默认值000的需求,需要结合shift和modify-default-beta操作完成。
正确的Jolt Spec
[ { "operation": "shift", "spec": { "name": "name", "phone": { "*": { "mobile": "customer.mobile" } } } }, { "operation": "modify-default-beta", "spec": { "customer": { "mobile": "000" } } } ]
场景验证
场景1:phone为数组输入
输入:
{ "name": "Fred", "phone": [ { "mobile": "1112223333" } ] }
输出:
{ "name" : "Fred", "customer" : { "mobile" : "1112223333" } }
场景2:phone为null输入
输入:
{ "name": "Fred", "phone": null }
输出:
{ "name" : "Fred", "customer" : { "mobile" : "000" } }
逻辑说明
- shift操作:负责将正常
phone数组中的mobile值映射到customer.mobile,同时保留name字段;当phone为null时,这一步不会生成customer.mobile节点。 - modify-default-beta操作:检查
customer.mobile是否存在,若不存在(对应phone为null的场景),则将其设置为默认值000。
内容的提问来源于stack exchange,提问作者VIshal Choudhary
相关产品推荐
相关产品推荐

