Jolt转换中tempName与name字段赋值规则问题排查及修正
问题背景
输入说明:空值与Null字符串已被正确处理,但tempName与name字段的赋值存在问题。
规则要求:
- 若tempName为空字符串或Null,name字段值保持不变;
- 若tempName为有效值(如"abc"),name字段需设置为该有效值。
输入示例(tempName为空字符串,name为Test1)
[ { "bI": { "id": "t1", "locale": [ "id_ID", "id_ID" ], "tempName": "", "name": "Test1", "isActive": false, "pT": "Re" }, "id": "t1", "ContextID": "id_ID", "cf": { "name": "asd" }, "dE": { "sS": "arte" }, "mk": {}, "pd": {} } ]
现有处理tempName空值的Spec(已符合预期)
[ { "operation": "shift", "spec": { "*": { "*": "[&1].&", "bI": { "*": "[&2].bI.&", "tempName": { "": "[&3].&2.&1", "*": { "@1": "[&4].&3.&2" } } } } } } ]
已实现场景
- 场景1:tempName为空字符串时,输出中tempName被设为Null,name保持Test1;
- 场景2:tempName为"abc"时,输出中tempName保持"abc",name仍为Test1。
未实现场景
- 场景3:tempName为空或Null时,name值需保持原样;
- 场景4:tempName为"abc"时,name需设为"abc"。
现有最终Spec(未解决场景3、4)
[ { "operation": "shift", "spec": { "*": { "*": "[&1].&", "bI": { "*": "[&2].bI.&", "tempName": { "": "[&3].&2.&1", "*": { "@1": "[&4].&3.&2" } } } } } }, { "operation": "modify-overwrite-beta", "spec": { "*": { "name": "=notNull(@(1,tempName))" } } }, { "operation": "remove", "spec": { "*": { "tempName": "" } } } ]
预期输出
当tempName为空字符串时:
[ { "bI" : { "id" : "t1", "locale" : [ "id_ID", "id_ID" ], "tempName" : null, "name" : "Test1", "isActive" : false, "pT" : "Re" }, "id" : "t1", "ContextID" : "id_ID", "cf" : { "name" : "asd" }, "dE" : { "sS" : "arte" }, "mk" : { }, "pd" : { } } ]
当tempName为"abc"时:
[ { "bI" : { "id" : "t1", "locale" : [ "id_ID", "id_ID" ], "tempName" : "abc", "name" : "abc", "isActive" : false, "pT" : "Re" }, "id" : "t1", "ContextID" : "id_ID", "cf" : { "name" : "asd" }, "dE" : { "sS" : "arte" }, "mk" : { }, "pd" : { } } ]
修正后的最终Spec
[ { "operation": "shift", "spec": { "*": { "*": "[&1].&", "bI": { "*": "[&2].bI.&", "tempName": { "": "[&3].&2.&1", "*": { "@1": "[&4].&3.&2" } } } } } }, { "operation": "modify-overwrite-beta", "spec": { "*": { "bI": { "name": "=ifElse(notNull(@(1,tempName)), @(1,tempName), @(1,name))" } } } } ]
修正说明
- 移除原有
remove操作,因为预期输出需要保留tempName字段(空值时显示为null); - 调整
modify-overwrite-beta规则:通过ifElse条件判断,当tempName非空时,将name替换为tempName的值;当tempName为空或null时,保留name原有值,完全匹配规则要求。
内容的提问来源于stack exchange,提问作者Akhila REDDY
相关产品推荐
相关产品推荐

