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

如何使用Jolt Spec转换将常量数组追加到目标数组

Jolt转换实现数组元素追加需求

输入JSON

[
  {
    "id": "id1",
    "projectId": "ALL",
    "composable": "true",
    "schema": {
      "properties": {},
      "required": [ "name", "db_access" ]
    }
  },
  {
    "id": "id2",
    "projectId": "ALL",
    "composable": "true",
    "schema": {
      "properties": {},
      "required": [ "test", "remote" ]
    }
  }
]

期望输出JSON

[
  {
    "id": "id1",
    "projectId": "ALL",
    "composable": "true",
    "schema": {
      "properties": {},
      "required": [ "name", "db_access", "account", "region" ]
    }
  },
  {
    "id": "id2",
    "projectId": "ALL",
    "composable": "true",
    "schema": {
      "properties": {},
      "required": [ "test", "remote", "account", "region" ]
    }
  }
]

Jolt转换规则

追加到数组末尾

要将"account"和"region"追加到每个对象的schema.required数组末尾,可使用以下Jolt Spec:

[
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "*": {
        "schema": {
          "required": ["@(1,schema.required)", "account", "region"]
        }
      }
    }
  }
]

追加到数组开头

如果需要将两个元素放到数组开头,只需调整元素顺序即可:

[
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "*": {
        "schema": {
          "required": ["account", "region", "@(1,schema.required)"]
        }
      }
    }
  }
]

规则解释

  • operation: "modify-overwrite-beta":使用modify操作的beta版本,支持数组拼接这类复杂修改逻辑
  • "*":匹配输入数组中的每一个对象
  • "required": ["@(1,schema.required)", "account", "region"]:
    • @(1,schema.required):向上回溯1层(当前对象层级),引用原有的schema.required数组内容
    • 按指定顺序拼接原有数组和新增元素,最终覆盖原有的required数组

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.12 16:25:23