You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用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": {
      "*": "[]"
    }
  }
]

配置说明

  1. 第一层shift操作:

    • 遍历外层数组的每个元素(*),再遍历每个元素内的positionIds数组(内层*)。
    • @2,cabinetId:向上两层获取当前外层元素的cabinetId值;@2,anotherId同理。
    • @:指代当前遍历到的positionIds数组元素值。
    • [&3]:向上三层引用外层数组的索引,确保同一原始元素下的所有positionId都映射到对应索引的对象集合中。
  2. 第二层shift操作:

    • 将第一层生成的索引式对象结构,展开为扁平的数组,最终得到每条独立的记录。

输出结果

[
  {
    "cabinetId": "C1",
    "anotherId": "A1",
    "positionId": "P1"
  },
  {
    "cabinetId": "C1",
    "anotherId": "A1",
    "positionId": "P2"
  },
  {
    "cabinetId": "C2",
    "anotherId": "A2",
    "positionId": "P3"
  }
]

内容的提问来源于stack exchange,提问作者takotsubo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.19 08:40:32