使用Jolt提取数组值并展开嵌套数组的技术问题
正确的Jolt配置:展开positionIds数组生成独立记录
输入JSON示例
[ { "cabinetId": "C1", "anotherId": "A1", "positionIds": ["P1", "P2"] }, { "cabinetId": "C2", "anotherId": "A2", "positionIds": ["P3"] } ]
错误配置问题说明
如果使用单层shift操作,比如:
[ { "operation": "shift", "spec": { "*": { "cabinetId": "[#2].cabinetId", "anotherId": "[#2].anotherId", "positionIds": "[#2].positionId" } } } ]
会导致positionIds数组未被展开,仍然以数组形式保留,无法实现每个数组元素对应独立记录的需求。
正确Jolt配置
[ { "operation": "shift", "spec": { "*": { "positionIds": { "*": { "@2,cabinetId": "[&3].cabinetId", "@2,anotherId": "[&3].anotherId", "@": "[&3].positionId" } } } } }, { "operation": "shift", "spec": { "*": "[]" } } ]
配置说明
第一层shift操作:
- 遍历外层数组的每个元素(
*),再遍历每个元素内的positionIds数组(内层*)。 @2,cabinetId:向上两层获取当前外层元素的cabinetId值;@2,anotherId同理。@:指代当前遍历到的positionIds数组元素值。[&3]:向上三层引用外层数组的索引,确保同一原始元素下的所有positionId都映射到对应索引的对象集合中。
- 遍历外层数组的每个元素(
第二层shift操作:
- 将第一层生成的索引式对象结构,展开为扁平的数组,最终得到每条独立的记录。
输出结果
[ { "cabinetId": "C1", "anotherId": "A1", "positionId": "P1" }, { "cabinetId": "C1", "anotherId": "A1", "positionId": "P2" }, { "cabinetId": "C2", "anotherId": "A2", "positionId": "P3" } ]
内容的提问来源于stack exchange,提问作者takotsubo
相关产品推荐
相关产品推荐

