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

Jolt JSON数组转换求助:重复用户条目被合并问题

Jolt处理重复用户条目时保留独立对象的解决方案

问题核心

输入JSON包含重复用户条目(如Yearly产品中的Joe Hashashi、Monthly产品中的Leo Biz各两条),现有Jolt Spec会将重复内容合并为单个对象,无法保留每个条目作为独立对象输出。

输入JSON

[
  {
    "product": "Yearly",
    "user": {
      "name": "Joe Hashashi",
      "id": "101"
    },
    "amount": 99.99
  },
  {
    "product": "Yearly",
    "user": {
      "name": "Joe Hashashi",
      "id": "101"
    },
    "amount": 99.99
  },
  {
    "product": "Monthly",
    "user": {
      "name": "Leo Biz",
      "id": "102"
    },
    "amount": 9.99
  },
  {
    "product": "Monthly",
    "user": {
      "name": "Leo Biz",
      "id": "102"
    },
    "amount": 9.99
  }
]

尝试的Jolt Spec(导致合并)

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "product": "@(1,user.id).plan",
        "user": {
          "name": "@(2,user.id).customer.name",
          "id": "@(2,user.id).customer.id"
        },
        "amount": "@(1,user.id).cost"
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*": "[]"
    }
  }
]

当前输出(合并后)

[
  {
    "plan": "Yearly",
    "customer": {
      "name": "Joe Hashashi",
      "id": "101"
    },
    "cost": 99.99
  },
  {
    "plan": "Monthly",
    "customer": {
      "name": "Leo Biz",
      "id": "102"
    },
    "cost": 9.99
  }
]

期望输出(保留独立条目)

[
  {
    "plan": "Yearly",
    "customer": {
      "name": "Joe Hashashi",
      "id": "101"
    },
    "cost": 99.99
  },
  {
    "plan": "Yearly",
    "customer": {
      "name": "Joe Hashashi",
      "id": "101"
    },
    "cost": 99.99
  },
  {
    "plan": "Monthly",
    "customer": {
      "name": "Leo Biz",
      "id": "102"
    },
    "cost": 9.99
  },
  {
    "plan": "Monthly",
    "customer": {
      "name": "Leo Biz",
      "id": "102"
    },
    "cost": 9.99
  }
]

修正后的Jolt Spec

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "product": "[&1].plan",
        "user": {
          "name": "[&2].customer.name",
          "id": "[&2].customer.id"
        },
        "amount": "[&1].cost"
      }
    }
  }
]

原理说明

你之前的Spec使用@(1,user.id)作为输出对象的键,Jolt会将相同ID的条目合并到同一键下,导致重复内容被覆盖。修正后的Spec改用[&1](代表输入数组当前条目的索引)作为输出数组的位置标识,每个条目会被映射到输出数组的对应索引位置,完全保留原始数组的条目数量和顺序,不会因为内容重复而合并。

如果你的场景有更复杂的嵌套转换,核心原则始终是:避免使用重复字段作为输出对象的键,依赖原始数组的层级索引(&1、&2等)来确保每个条目独立存在。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.18 22:05:00