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

如何通过Jolt根据id数组与映射提取值生成对象数组?

基于Jolt实现动态ID关联的对象数组转换

需求

根据输入JSON里的access_id数组生成新的对象数组,将textFields中以comment_、key_、type_前缀加ID命名的字段,对应归属到对应ID的对象中,输入支持多个动态ID。

输入JSON

{
  "textFields": {
    "comment_12345": "Test comment A",
    "comment_67890": "Test comment B",
    "key_12345": "key A",
    "key_67890": "key B",
    "type_12345": "CVC",
    "type_67890": "AVF"
  },
  "access_id": [
    "12345",
    "67890"
  ]
}

预期输出JSON

{
  "data": [
    {
      "id": "12345",
      "comment": "Test comment A",
      "key": "key A",
      "type": "CVC"
    },
    {
      "id": "67890",
      "comment": "Test comment B",
      "key": "key B",
      "type": "AVF"
    }
  ]
}

Jolt转换规范

[
  {
    "operation": "shift",
    "spec": {
      "access_id": {
        "*": {
          "@": "data[&1].id",
          "@(2,textFields.comment_&)": "data[&1].comment",
          "@(2,textFields.key_&)": "data[&1].key",
          "@(2,textFields.type_&)": "data[&1].type"
        }
      }
    }
  }
]

转换逻辑说明

  1. 遍历access_id数组的每个元素,用&1指代当前数组元素的索引
  2. 将当前ID直接映射到data[索引].id字段
  3. 通过@(2,textFields.comment_&)向上回溯2层到根节点,取出textFields中对应comment_+当前ID的字段值,映射到data[索引].comment,同理完成key和type字段的关联映射

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.30 00:07:23