修复JOLT配置:为stat.item数组所有元素添加object.id字段
修复JOLT配置为所有item元素添加id字段
你的问题出在shift操作中id字段的路径映射上:原配置里"id": "[&4].id"的&4指向最外层数组的索引(仅第一个位置),所以id只会被赋值给结果数组的第一个元素,无法覆盖所有item项。
修复方案一(单步shift)
直接在遍历item数组时,引用上层的object.id值,确保每个item元素都能获取到id:
[ { "operation": "shift", "spec": { "*": { "content": { "statUniqueObject": { "stat": { "item": { "*": { // 保留item自身的所有字段 "*": "[&1].&", // 通过@(层级,路径)获取上层的object.id,&1对应当前item的数组索引 "@(2,object.id)": "[&1].id" } } } } } } } } ]
修复方案二(两步shift,逻辑更清晰)
先将object.id和item数组分别提取到临时节点,再遍历数组为每个元素添加id:
[ // 第一步:提取临时变量和item数组 { "operation": "shift", "spec": { "*": { "content": { "statUniqueObject": { "object": { "id": "tempId" }, "stat": { "item": "items" } } } } } }, // 第二步:遍历item数组,为每个元素添加id { "operation": "shift", "spec": { "items": { "*": { "*": "[&1].&", "@(2,tempId)": "[&1].id" } } } } ]
转换后结果
两种方案都会得到符合预期的输出:
[ { "exp": 34725, "click": 789, "reach": 331399, "date": "2025-04-05", "id": 810069 }, { "exp": 32090, "click": 747, "reach": 344560, "date": "2025-04-06", "id": 810069 } ]
内容的提问来源于stack exchange,提问作者dogyears
相关产品推荐
相关产品推荐

