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

无法通过Jolt Transform提取嵌套JSON内的ccc_id与ddd_id,求助

Jolt Transform脚本问题排查与修正

问题背景

给定输入JSON数组:

[
  {
    "aaa": "-",
    "name": "something_val",
    "bbb": "-",
    "is_active": true,
    "repository": null,
    "image_url": "some_val",
    "ccc_id_tmp": 2,
    "ddd_id_tmp": 3,
    "ccc_created_at": "timestamp",
    "updated_at": "timestamp",
    "result_ccc_id_lookup": {
      "ccc_id": "some_uuid"
    },
    "result_ddd_id_lookup": {
      "ddd_id": "some_uuid"
    }
  },
  {
    "aaa": "hi",
    "name": "something_val",
    "bbb": "yooo",
    "is_active": false,
    "repository": null,
    "image_url": "some_val",
    "ccc_id_tmp": 4,
    "ddd_id_tmp": 5,
    "ccc_created_at": "timestamp",
    "updated_at": "timestamp",
    "result_ccc_id_lookup": {
      "ccc_id": "some_uuid_2"
    },
    "result_ddd_id_lookup": {
      "ddd_id": "some_uuid_2"
    }
  }
]

目标是提取result_ccc_id_lookup.ccc_id和result_ddd_id_lookup.ddd_id到顶层,预期输出(以第一个元素为例):

{
  "aaa": "-",
  "name": "something_val",
  "bbb": "-",
  "is_active": true,
  "repository": null,
  "image_url": "some_val",
  "ccc_id_tmp": 2,
  "ddd_id_tmp": 3,
  "ccc_created_at": "timestamp",
  "updated_at": "timestamp",
  "ccc_id": "some_uuid",
  "ddd_id": "some_uuid"
}

当前使用的脚本执行后,实际输出缺少ccc_id和ddd_id字段:

{
  "aaa": "-",
  "name": "something_val",
  "bbb": "-",
  "is_active": true,
  "repository": null,
  "image_url": "some_val",
  "ccc_id_tmp": 2,
  "ddd_id_tmp": 3
}

问题分析

你的脚本有两个关键问题:

  1. 嵌套字段的层级引用错误:在result_ccc_id_lookup和result_ddd_id_lookup内部使用[&1]时,&1指代的是当前嵌套对象的父级(也就是result_ccc_id_lookup这个键的位置),而非外层数组的索引。需要用&2来向上跳两层,指向外层数组的元素索引。
  2. 冗余字段定义:脚本里写了"objective": "[&1].objective",但原输入JSON中并没有objective字段,这部分属于无效配置,可删除。

修正后的Jolt脚本

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "aaa": "[&1].aaa",
        "name": "[&1].name",
        "bbb": "[&1].bbb",
        "is_active": "[&1].is_active",
        "repository": "[&1].repository",
        "image_url": "[&1].image_url",
        "ccc_id_tmp": "[&1].ccc_id_tmp",
        "ddd_id_tmp": "[&1].ddd_id_tmp",
        "ccc_created_at": "[&1].ccc_created_at",
        "updated_at": "[&1].updated_at",
        "result_ccc_id_lookup": {
          "ccc_id": "[&2].ccc_id"
        },
        "result_ddd_id_lookup": {
          "ddd_id": "[&2].ddd_id"
        }
      }
    }
  }
]

验证结果

执行修正后的脚本后,输出的JSON数组中每个元素都会包含ccc_id和ddd_id字段,完全符合预期:

[
  {
    "aaa": "-",
    "name": "something_val",
    "bbb": "-",
    "is_active": true,
    "repository": null,
    "image_url": "some_val",
    "ccc_id_tmp": 2,
    "ddd_id_tmp": 3,
    "ccc_created_at": "timestamp",
    "updated_at": "timestamp",
    "ccc_id": "some_uuid",
    "ddd_id": "some_uuid"
  },
  {
    "aaa": "hi",
    "name": "something_val",
    "bbb": "yooo",
    "is_active": false,
    "repository": null,
    "image_url": "some_val",
    "ccc_id_tmp": 4,
    "ddd_id_tmp": 5,
    "ccc_created_at": "timestamp",
    "updated_at": "timestamp",
    "ccc_id": "some_uuid_2",
    "ddd_id": "some_uuid_2"
  }
]

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.06 21:10:06