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

如何通过Jolt将amountVar值插入amount数组第2个位置?

Jolt转换:将amountVar插入到amount数组的第二个位置

需求:把每个嵌套对象中的amountVar值插入到amount数组的第2个位置(索引1处),并移除amountVar字段。

输入JSON

{"PTL" : {"TA" : [ {"T" : [ {"amount" : [ 10, 11, 21 ],"amountVar" : 7} ]}, {"T" : [ {"amount" : [ 20, 22, 22 ],"amountVar" : 6} ]}, {"T" : [ {"amount" : [ 30, 33, 23 ],"amountVar" : 16} ]} ]}}

已尝试的Jolt转换规则

{"operation": "modify-overwrite-beta","spec": {"PTL": {"TA": {"*": {"T": {"*": {"amount": ["@(1,amount[0])","@(1,amountVar)","@(1,amount[1])","@(1,amount[2])"]}}}}}}}

期望输出JSON

{"PTL" : {"TA" : [ {"T" : [ {"amount" : [ 10, 7, 11, 21 ]} ]}, {"T" : [ {"amount" : [ 20, 6, 22, 25 ]} ]}, {"T" : [ {"amount" : [ 30, 16, 33, 23 ]} ]} ]}}

修正后的Jolt转换规则

原有规则已实现数组插入逻辑,但缺少移除amountVar的步骤,完整规则如下:

[
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "PTL": {
        "TA": {
          "*": {
            "T": {
              "*": {
                "amount": ["@(1,amount[0])", "@(1,amountVar)", "@(1,amount[1])", "@(1,amount[2])"]
              }
            }
          }
        }
      }
    }
  },
  {
    "operation": "remove",
    "spec": {
      "PTL": {
        "TA": {
          "*": {
            "T": {
              "*": {
                "amountVar": ""
              }
            }
          }
        }
      }
    }
  }
]

说明

  • modify-overwrite-beta操作完成数组拼接,将amountVar插入到amount数组的指定位置;
  • remove操作删除不再需要的amountVar字段;
  • 期望输出中第二个amount数组的最后一个元素为25,输入中对应值为22,推测是笔误,上述规则会保留输入原有的数组元素值。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.17 14:39:57