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

求符合要求的JOLT Shift规范:整合对象并替换值

JOLT转换解决方案及数组处理提示

输入JSON

{
  "items": [
    {
      "description": "myDescription",
      "attributes": [
        {
          "identifier": "colour",
          "type": "attribute",
          "values": [
            "DE"
          ],
          "hint": "1"
        },
        {
          "identifier": "pin",
          "type": "attribute",
          "values": [
            "4711"
          ],
          "hint": "1"
        },
        {
          "identifier": "price",
          "type": "price",
          "values": [
            "77.27"
          ],
          "hint": "1"
        },
        {
          "identifier": "weight",
          "type": "attribute",
          "values": [
            "0.47"
          ],
          "hint": "1"
        },
        {
          "identifier": "status",
          "type": "attribute",
          "values": [
            "active"
          ],
          "hint": "1"
        }
      ]
    }
  ],
  "temp": {
    "hint": "1",
    "identifier": "additionalDescription",
    "values": [
      {
        "lang": "de",
        "value": "temp"
      },
      {
        "lang": "en",
        "value": "temp"
      },
      {
        "lang": "fr",
        "value": "temp"
      },
      {
        "lang": "it",
        "value": "temp"
      },
      {
        "lang": "es",
        "value": "temp"
      }
    ],
    "type": "attribute"
  }
}

期望输出JSON

{
  "items": [
    {
      "description": "myDescription",
      "attributes": [
        {
          "identifier": "colour",
          "type": "attribute",
          "values": [
            "DE"
          ],
          "hint": "1"
        },
        {
          "identifier": "pin",
          "type": "attribute",
          "values": [
            "4711"
          ],
          "hint": "1"
        },
        {
          "identifier": "price",
          "type": "price",
          "values": [
            "77.27"
          ],
          "hint": "1"
        },
        {
          "identifier": "weight",
          "type": "attribute",
          "values": [
            "0.47"
          ],
          "hint": "1"
        },
        {
          "identifier": "status",
          "type": "attribute",
          "values": [
            "active"
          ],
          "hint": "1"
        },
        {
          "hint": "1",
          "identifier": "additionalDescription",
          "values": [
            {
              "lang": "de",
              "value": "myDescription"
            },
            {
              "lang": "en",
              "value": "myDescription"
            },
            {
              "lang": "fr",
              "value": "myDescription"
            },
            {
              "lang": "it",
              "value": "myDescription"
            },
            {
              "lang": "es",
              "value": "myDescription"
            }
          ],
          "type": "attribute"
        }
      ]
    }
  ]
}

需求说明

  • 将顶层的temp对象整合到items[0].attributes数组末尾
  • 将temp.values中所有对象的value字段值替换为对应item的description值

修正后的JOLT规范

[
  {
    "operation": "shift",
    "spec": {
      "items": {
        "*": {
          // 保留原description字段
          "description": "&2[&1].description",
          // 保留原attributes数组的所有元素
          "attributes": {
            "*": "&3[&2].attributes[]"
          },
          // 处理temp对象:替换value并追加到attributes数组
          "@(3,temp)": {
            "hint": "&4[&3].attributes[#2].hint",
            "identifier": "&4[&3].attributes[#2].identifier",
            "type": "&4[&3].attributes[#2].type",
            "values": {
              "*": {
                "lang": "&6[&5].attributes[#4].values[&1].lang",
                // 引用当前item的description替换原value
                "@(4,description)": "&6[&5].attributes[#4].values[&1].value"
              }
            }
          }
        }
      }
    }
  }
]

数组处理实用提示

  • 数组元素追加:在RHS规则末尾加[],比如&3.attributes[],可将匹配元素追加到目标数组,避免覆盖原有内容
  • 跨层级取值:用@(n, path)格式回溯取值,n表示向上回溯的层级数,比如@(4,description)表示回溯4级获取当前item的description字段值
  • 数组遍历匹配:用*匹配数组的所有元素,比如attributes.*遍历attributes数组的每个元素,temp.values.*遍历values数组的每个对象
  • 动态数组位置:#符号表示当前数组元素的索引,比如#2可定位新元素在目标数组中的位置

内容的提问来源于stack exchange,提问作者Katja Bürger

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.24 19:47:07