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

使用JOLT提取数组首元素键值并复制至其余元素

JOLT转换规则实现指定JSON数组修改

输入JSON

[
  {
    "key1": "11",
    "key2": "20"
  },
  {
    "key1": "13"
  },
  {
    "key1": "10"
  }
]

期望输出JSON

[
  {
    "key1": "1",
    "key2": "2"
  },
  {
    "key1": "13",
    "key2": "2"
  },
  {
    "key1": "10",
    "key2": "2"
  }
]

JOLT转换规则

[
  {
    "operation": "shift",
    "spec": {
      "0": {
        "key2": "sharedKey2",
        "key1": "processed[0].key1"
      },
      "*": {
        "key1": "processed[&1].key1"
      }
    }
  },
  {
    "operation": "modify-default-beta",
    "spec": {
      "sharedKey2": "=replace(@(1), '20', '2')",
      "processed": {
        "0": {
          "key1": "1"
        },
        "*": {
          "key2": "@(2,sharedKey2)"
        }
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "processed": "[]"
    }
  }
]

规则说明

  1. Shift操作(第一步):
    • 提取原数组第一个元素的key2值暂存为sharedKey2,同时将所有元素的key1保留到processed数组中,保持原索引对应关系。
  2. Modify操作(第二步):
    • 将暂存的sharedKey2从输入的"20"替换为目标值"2";
    • 修改processed数组第一个元素的key1为"1";
    • 给processed数组的每个元素添加key2字段,值引用上层修改后的sharedKey2。
  3. Shift操作(第三步):
    • 将处理完成的processed数组映射为最终的输出数组结构。

内容的提问来源于stack exchange,提问作者Будён Михайлович Семённый

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.14 16:43:13