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

如何通过Jolt Transform为JSON嵌套数组添加唯一标识符?

用Jolt Transform给JSON嵌套数组加唯一标识符

要给JSON里的嵌套数组元素添上唯一标识符,这样数组拆分后还能按原顺序重新拼回去。

输入JSON

{
  "column1": "value1",
  "column2": {
    "item": [
      {
        "itemname": "x"
      },
      {
        "itemname": "y"
      },
      {
        "itemname": "z"
      }
    ]
  },
  "column3": [
    {
      "itemcat": "a"
    },
    {
      "itemcat": "b"
    },
    {
      "itemcat": "c"
    }
  ]
}

期望输出JSON

{
  "column1": "value1",
  "column2": {
    "item": [
      {
        "itemname": "x",
        "identifier": "item_0"
      },
      {
        "itemname": "y",
        "identifier": "item_1"
      },
      {
        "itemname": "z",
        "identifier": "item_2"
      }
    ]
  },
  "column3": [
    {
      "itemcat": "a",
      "identifier": "column3_0"
    },
    {
      "itemcat": "b",
      "identifier": "column3_1"
    },
    {
      "itemcat": "c",
      "identifier": "column3_2"
    }
  ]
}

对应的Jolt转换规则

[
  {
    "operation": "shift",
    "spec": {
      "column1": "column1",
      "column2": {
        "item": {
          "*": {
            "@": "column2.item[&1]",
            "$": "column2.item[&1].identifier"
          }
        }
      },
      "column3": {
        "*": {
          "@": "column3[&1]",
          "$": "column3[&1].identifier"
        }
      }
    }
  },
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "column2": {
        "item": {
          "*": {
            "identifier": "=concat('item_', @(1,identifier))"
          }
        }
      },
      "column3": {
        "*": {
          "identifier": "=concat('column3_', @(1,identifier))"
        }
      }
    }
  }
]

规则解释

  • Shift步骤:遍历两个目标数组的每一个元素,先保留原有字段,再把当前元素的数组索引(&1代表当前层级的索引值)存到临时的identifier字段里。
  • Modify步骤:用concat函数把数组名称和索引拼接成最终的唯一标识,比如item_0、column3_2这种格式,确保每个元素的标识符唯一且能体现原顺序。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.15 18:15:13