编写Jolt规格移除externalIds数组中重复的externalId元素
Jolt 规格实现移除重复的externalId元素
输入JSON
{ "id": "id_1", "targetId": "targetId42", "externalId": "1extid", "attributes": { "uniqueInfo": { "externalIds": [ "3extid", "2extid", "4extid", "1extid", "5extid" ] } } }
期望输出JSON
{ "id": "id_1", "targetId": "targetId42", "externalId": "1extid", "attributes": { "uniqueInfo": { "externalIds": [ "3extid", "2extid", "4extid", "5extid" ] } } }
Jolt 规格
[ { "operation": "shift", "spec": { "id": "id", "targetId": "targetId", "externalId": "externalId", "attributes": { "uniqueInfo": { "externalIds": { "*": { "@": "[#3].attributes.uniqueInfo.externalIds[#2]", "$": "[#3].temp[#2]", "@(4,externalId)": "[#3].matchVal" } } } } } }, { "operation": "modify-overwrite-beta", "spec": { "*": { "attributes": { "uniqueInfo": { "externalIds": "=filter(@(3,temp),@(3,matchVal))" } }, "temp": "", "matchVal": "" } } }, { "operation": "shift", "spec": { "*": "" } } ]
规格说明
- Shift 操作:映射原数据的所有字段,同时将
externalIds数组的每个元素、对应索引值,以及顶层externalId存入临时字段,为过滤做准备。 - Modify-overwrite-beta 操作:通过
filter函数筛选出不等于顶层externalId的数组元素,随后清空临时字段。 - 最终 Shift 操作:扁平化处理后的结构,得到目标输出。
内容的提问来源于stack exchange,提问作者ayan_2587
相关产品推荐
相关产品推荐

