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

寻求JOLT转换方案:用实体替换引用ID

需求:JOLT转换实现引用ID替换为对应实体

输入JSON

{
  "users": [
    {
      "id": 1,
      "firstname": "Max"
    },
    {
      "id": 4,
      "firstname": "Mel"
    }
  ],
  "special-role": {
    "id": 4
  }
}

期望输出JSON

{
  "special-role" : {
     "firstname": "Mel"
  }
}

问题卡点

之前尝试的规则里用了固定值4来匹配users中的数据,但实际需要动态引用special-role.id的值,不知道怎么替换这个固定参数。

正确的JOLT转换规则

[
  {
    "operation": "shift",
    "spec": {
      "users": {
        "*": {
          "firstname": "user_map.@(1,id).firstname"
        }
      },
      "special-role": {
        "id": "target_id"
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "user_map": {
        "*": {
          "firstname": {
            "@(3,target_id)": {
              "$": "special-role.firstname"
            }
          }
        }
      }
    }
  },
  {
    "operation": "remove",
    "spec": {
      "user_map": "",
      "target_id": ""
    }
  }
]

规则说明

  1. 第一步shift:把users数组转成以id为键的user_map结构,同时把special-role的id暂存到target_id,方便后续动态匹配。
  2. 第二步shift:遍历user_map的所有键,当键和target_id的值一致时,将对应的firstname赋值给special-role.firstname。
  3. 第三步remove:清理中间生成的user_map和target_id,只保留最终需要的结构。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.05 04:35:03