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

Jolt转换:如何将对象转为指定结构的数组

Jolt转换:生成嵌套对象数组的正确规范

你之前的转换规范把所有事件字段直接推入incidents数组,导致输出是平铺的数值/字符串列表。要生成包含独立对象的数组,需要利用Jolt的索引引用功能来分组每个事件的字段。

输入数据

{"cluster_id": "1","data": {"id": 1,"types": [{"incident_id": 10,"incident_ref": "AAA","incident_code": "123","incident_date": "2010-11-15T00:01:00Z"},{"incident_id": 20,"incident_ref": "BBB","incident_code": "456","incident_date": "2020-11-15T00:01:00Z"}]}}

目标输出

{"id" : "1","incidents" : [{"id": 10, "ref": "AAA", "code": "123", "date": "2010-11-15T00:01:00Z"},{"id": 20, "ref": "BBB", "code": "456", "date": "2020-11-15T00:01:00Z"}]}

正确的Jolt转换规范

使用shift操作,通过数组索引占位符&1来确保每个types数组元素对应生成incidents数组里的独立对象:

[
  {
    "operation": "shift",
    "spec": {
      "cluster_id": "id",
      "data": {
        "types": {
          "*": {
            "incident_id": "incidents[&1].id",
            "incident_ref": "incidents[&1].ref",
            "incident_code": "incidents[&1].code",
            "incident_date": "incidents[&1].date"
          }
        }
      }
    }
  }
]

规范说明

  • cluster_id直接映射到顶层字段id,和你之前的写法一致。
  • data.types.*遍历原数组中的每一个事件对象,*代表当前元素的数组索引。
  • &1引用当前层级的索引值(也就是*对应的数组位置),这样每个事件的字段会被归入incidents数组对应索引的对象中,确保每个事件的字段被分组到同一个对象里。
  • 每个原字段(incident_id、incident_ref等)分别映射到目标对象的对应键名(id、ref等),完成字段重命名。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.11 18:40:29