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

如何通过Jolt实现两个列表元素全组合并生成新数组条目

Jolt实现两个列表的全量字段组合

需求说明

将attributes列表中的每个条目,与data列表中的每个条目进行全量组合,生成的新条目包含双方所有字段。

输入数据

{
  "data": [
    {
      "NAN_KEY": 1,
      "TEMP": 3
    },
    {
      "NAN_KEY": 123,
      "TEMP": 1243523
    }
  ],
  "attributes": [
    {
      "name": "attribute1",
      "value": 3
    },
    {
      "name": "attribute2",
      "value": 2
    }
  ]
}

期望输出

{
"attributes": [
    {
      "name": "attribute1",
      "value": 3,
      "NAN_KEY": 1,
      "TEMP": 3
    },
    {
      "name": "attribute1",
      "value": 3,
      "NAN_KEY": 123,
      "TEMP": 1243523
    },
    {
      "name": "attribute2",
      "value": 2,
      "NAN_KEY": 1,
      "TEMP": 3
    },
    {
      "name": "attribute2",
      "value": 2,
      "NAN_KEY": 123,
      "TEMP": 1243523
    }
  ]
}

Jolt转换规则

[
  {
    "operation": "shift",
    "spec": {
      "attributes": {
        "*": {
          "@2,data": {
            "*": {
              "@(3,name)": "[&4].name",
              "@(3,value)": "[&4].value",
              "$": "[&4].&",
              "@": "[&4].&"
            }
          }
        }
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*": "attributes[]"
    }
  }
]

规则说明

  1. 第一个shift操作:遍历attributes和data的所有元素,完成字段映射
    • @2,data:向上两层获取data数组,实现跨数组的全量遍历
    • [&4]:生成唯一的数组索引,确保每个组合对应独立的位置
    • @(3,name)/@(3,value):获取当前attributes条目的name和value字段
    • $/@:分别获取data条目的键和对应值
  2. 第二个shift操作:将临时生成的数组整理为最终的attributes数组结构

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.27 18:13:03