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

如何调整Jolt Spec以实现正确的字段列表配对转换输出?

Jolt Spec调整:关联testKey与testValue生成正确数组结构

我正在更新Jolt Spec以生成正确输出,首次尝试后发现Spec存在缺失,无法得到理想结果。

JSON输入

{
  "id": 111,
  "uuid": "b4915b22-d8c0-47bd-832c-18c0",
  "testKey1[xxx]": "identifier_1234",
  "testKey2[yyyy]": "identifier_14",
  "name": "name1",
  "testValue1[xxx]": "10",
  "testValue2[yyy]": "12",
  "address": "S2271",
  "updateTime": "2024-01-18T10:35:00Z"
}

当前使用的Jolt Spec

[
  {
    "operation": "shift",
    "spec": {
      "uuid": "idBus",
      "id": "idTech",
      "name": "title",
      "address": "location",
      "updateTime": "lastUpdated",
      "testValue*": "myList&.value",
      "testKey*": "myList&.code"
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*": "&",
      "myList*": "myListOfFields[]"
    }
  }
]

实际输出结果

{
  "idBus": "b4915b22-d8c0-47bd-832c-18c0",
  "idTech": 111,
  "title": "name1",
  "location": "S2271",
  "lastUpdated": "2024-01-18T10:35:00Z",
  "myListOfFields": [
    {
      "value": "identifier_1234"
    },
    {
      "value": "identifier_14"
    },
    {
      "code": "10"
    },
    {
      "code": "12"
    }
  ]
}

期望输出结果

{
  "idBus": "b4915b22-d8c0-47bd-832c-18c0",
  "idTech": 111,
  "title": "name1",
  "location": "S2271",
  "lastUpdated": "2024-01-18T10:35:00Z",
  "myListOfFields": [
    {
      "code": "identifier_1234",
      "value": "10"
    },
    {
      "code": "identifier_14",
      "value": "12"
    }
  ]
}

修正后的Jolt Spec

[
  {
    "operation": "shift",
    "spec": {
      "uuid": "idBus",
      "id": "idTech",
      "name": "title",
      "address": "location",
      "updateTime": "lastUpdated",
      "testKey([0-9]+)\\[.*\\]": "myList[$1].code",
      "testValue([0-9]+)\\[.*\\]": "myList[$1].value"
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*": "&",
      "myList": {
        "*": "myListOfFields[]"
      }
    }
  }
]

说明

  1. 第一个shift操作:
    • 常规字段映射保持不变
    • 通过正则表达式testKey([0-9]+)\\[.*\\]提取testKey后的数字(如testKey1[xxx]中的1),将对应值放到myList[$1].code
    • 同理,testValue([0-9]+)\\[.*\\]提取数字后缀,将值放到myList[$1].value,这样相同数字后缀的testKey和testValue会被归到同一个对象中
  2. 第二个shift操作:将myList下的所有对象收集到myListOfFields数组,其他字段原样保留

原Spec的问题在于,仅用testValue*和testKey*映射时,无法关联同后缀的key和value,导致生成的数组元素是单独的code或value对象,而非配对后的完整结构。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.02 07:02:41