Jolt转换问题:按唯一modelId聚合attributeType与attributeValue
Jolt转换解决方案:按modelId分组并归集属性列表
转换规范
[ { "operation": "shift", "spec": { "*": { "modelId": "@(1,modelId).modelId", "attributeType": "@(1,modelId).attributes[#2].attributeType", "attributeValue": "@(1,modelId).attributes[#2].attributeValue" } } }, { "operation": "shift", "spec": { "*": "[]" } }, { "operation": "default", "spec": { "*": { "attributes": [] } } } ]
规范说明
- 第一阶段(shift):遍历输入数组的每个元素,以
modelId的值作为临时分组键,把相同modelId的attributeType和attributeValue归集到对应键下的attributes数组中,同时保留modelId字段。#2用于在同一modelId分组内按输入顺序生成数组索引,保证属性顺序和原输入一致。 - 第二阶段(shift):把第一阶段生成的键值对结构转换成数组形式,每个分组对应的值成为数组的一个元素,完成从分组映射到目标数组的转换。
- 第三阶段(default):为每个实体的
attributes字段设置默认空数组,避免出现某个modelId无属性时字段缺失的情况(可根据实际场景选择是否保留)。
输入示例
[ { "modelId": "Phone-A", "attributeType": "memory", "attributeValue": "8" }, { "modelId": "Phone-A", "attributeType": "category", "attributeValue": "phone" }, { "modelId": "Tab-B", "attributeType": "memory", "attributeValue": "16" }, { "modelId": "Tab-B", "attributeType": "category", "attributeValue": "tablet" } ]
输出示例
[ { "modelId": "Phone-A", "attributes": [ { "attributeType": "memory", "attributeValue": "8" }, { "attributeType": "category", "attributeValue": "phone" } ] }, { "modelId": "Tab-B", "attributes": [ { "attributeType": "memory", "attributeValue": "16" }, { "attributeType": "category", "attributeValue": "tablet" } ] } ]
内容的提问来源于stack exchange,提问作者user1635625
相关产品推荐
相关产品推荐

