如何通过Jolt将amountVar值插入amount数组第2个位置?
Jolt转换:将amountVar插入到amount数组的第二个位置
需求:把每个嵌套对象中的amountVar值插入到amount数组的第2个位置(索引1处),并移除amountVar字段。
输入JSON
{"PTL" : {"TA" : [ {"T" : [ {"amount" : [ 10, 11, 21 ],"amountVar" : 7} ]}, {"T" : [ {"amount" : [ 20, 22, 22 ],"amountVar" : 6} ]}, {"T" : [ {"amount" : [ 30, 33, 23 ],"amountVar" : 16} ]} ]}}
已尝试的Jolt转换规则
{"operation": "modify-overwrite-beta","spec": {"PTL": {"TA": {"*": {"T": {"*": {"amount": ["@(1,amount[0])","@(1,amountVar)","@(1,amount[1])","@(1,amount[2])"]}}}}}}}
期望输出JSON
{"PTL" : {"TA" : [ {"T" : [ {"amount" : [ 10, 7, 11, 21 ]} ]}, {"T" : [ {"amount" : [ 20, 6, 22, 25 ]} ]}, {"T" : [ {"amount" : [ 30, 16, 33, 23 ]} ]} ]}}
修正后的Jolt转换规则
原有规则已实现数组插入逻辑,但缺少移除amountVar的步骤,完整规则如下:
[ { "operation": "modify-overwrite-beta", "spec": { "PTL": { "TA": { "*": { "T": { "*": { "amount": ["@(1,amount[0])", "@(1,amountVar)", "@(1,amount[1])", "@(1,amount[2])"] } } } } } } }, { "operation": "remove", "spec": { "PTL": { "TA": { "*": { "T": { "*": { "amountVar": "" } } } } } } } ]
说明
modify-overwrite-beta操作完成数组拼接,将amountVar插入到amount数组的指定位置;remove操作删除不再需要的amountVar字段;- 期望输出中第二个
amount数组的最后一个元素为25,输入中对应值为22,推测是笔误,上述规则会保留输入原有的数组元素值。
内容的提问来源于stack exchange,提问作者Chandrika
相关产品推荐
相关产品推荐

