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

如何编写Jolt Shift转换规则,筛选出action为'remove'的条目?

Jolt转换规则:提取所有action为'remove'的条目

需求说明

从嵌套的JSON结构中,提取所有action属性值为remove的条目,最终组成一个扁平化的数组。

Jolt转换规则

[
  {
    "operation": "shift",
    "spec": {
      "item": {
        "*": {
          "action": {
            "remove": {
              "@(2)": "[]"
            }
          },
          "item": {
            "*": {
              "@": "[]"
            }
          }
        }
      }
    }
  },
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "*": {
        "item": ""
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*": {
        "id": "[&1].id",
        "action": "[&1].action"
      }
    }
  }
]

规则解释

  1. 第一步Shift操作:
    • 遍历顶层item数组的每个元素,判断其action是否为remove,若是则将整个元素存入结果数组(@(2)指向当前遍历的元素)。
    • 递归处理每个元素的子item数组,将子元素也加入结果数组,实现深层嵌套结构的遍历。
  2. Modify操作:
    • 清除结果数组中每个条目的item字段,避免嵌套结构残留。
  3. 第二步Shift操作:
    • 重新整理每个条目,仅保留id和action字段,与期望输出结构完全匹配。

输入验证

{
  "id": 11,
  "item": [
    {
      "id": "11_1",
      "action": "add",
      "item": [
        {
          "id": "11_1_1",
          "action": "add",
          "item": [
            {
              "id": "11_1_1_1",
              "action": "remove"
            }
          ]
        },
        {
          "id": "11_1_2",
          "action": "remove",
          "item": [
            {
              "id": "11_1_2_1",
              "action": "remove"
            }
          ]
        }
      ]
    }
  ]
}

输出结果

[
  {
    "id": "11_1_1_1",
    "action": "remove"
  },
  {
    "id": "11_1_2",
    "action": "remove"
  },
  {
    "id": "11_1_2_1",
    "action": "remove"
  }
]

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.20 20:12:49