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

如何修改Jolt Spec将根节点category加入attributes数组?

Jolt Transform:将根节点category字段并入attributes数组

输入JSON

{
  "id": 12345,
  "category": 2,
  "productAttributes": {
    "data": [
      {
        "name": "Size",
        "slug": "size",
        "value": "10"
      },
      {
        "name": "Color",
        "slug": "color",
        "value": "Grey"
      }
    ]
  }
}

现有Jolt Spec

[
  {
    "operation": "shift",
    "spec": {
      "productAttributes": {
        "data": {
          "*": {
            "slug": "attributes[#2].key",
            "value": "attributes[#2].value",
            "name": "attributes[#2].name"
          }
        }
      }
    }
  }
]

期望输出

{
  "attributes": [
    {
      "key": "size",
      "value": "10",
      "name": "Size"
    },
    {
      "key": "color",
      "value": "Grey",
      "name": "Color"
    },
    {
      "key": "category",
      "value": "2",
      "name": "Category"
    }
  ]
}

修改后的Jolt Spec

[
  {
    "operation": "shift",
    "spec": {
      "productAttributes": {
        "data": {
          "*": {
            "slug": "attributes[#2].key",
            "value": "attributes[#2].value",
            "name": "attributes[#2].name"
          }
        }
      },
      "category": {
        "$": "attributes[].value",
        "#category": "attributes[].key",
        "#Category": "attributes[].name"
      }
    }
  }
]

说明

  • 新增根节点category的映射规则,通过$引用原字段的值作为value;
  • 用#指定静态字符串作为key和name;
  • 使用attributes[]自动将新元素追加到数组末尾,无需手动指定索引,适配原数组元素数量变化的场景。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.24 16:32:44