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

如何通过Jolt转换从steps数组提取扁平对象并去除空值与索引

Jolt转换:提取嵌套steps数组生成扁平数组

源JSON

[
   {
      "id":212331532,
      "type":"number",
      "status":"Active",
      "steps":[
         {
            "id":297130964,
            "type":"url",
            "status":"Active"
         }
      ]
   },
   {
      "id":297130868,
      "type":"action",
      "status":"Active"
   },
   {
      "id":297130961,
      "type":"step",
      "status":"Active",
      "steps":[
         {
            "id":297130962,
            "type":"url",
            "is_retargeting":0
         },
         {
            "id":297130963,
            "type":"url",
            "is_retargeting":0
         }
      ]
   }
]

需求说明

提取所有嵌套在steps数组内的对象,整合成单一扁平数组,无steps数组的父对象直接跳过。

当前Jolt转换规则

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "steps": {
          "*": "[&2].&"
        }
      }
    }
  }
]

实际输出问题

  • 结果包含null空值条目
  • 对象被索引键(如"0"、"1")包裹,未形成预期的扁平数组

实际输出:

[ {
  "0" : {
    "id" : 297130964,
    "type" : "url",
    "status" : "Active"
  }
}, null, {
  "0" : {
    "id" : 297130962,
    "type" : "url",
    "is_retargeting" : 0
  },
  "1" : {
    "id" : 297130963,
    "type" : "url",
    "is_retargeting" : 0
  }
} ]

预期输出

[
   {
      "id":297130962,
      "type":"url",
      "is_retargeting":0
   },
   {
      "id":297130963,
      "type":"url",
      "is_retargeting":0
   },
   {
      "id":297130964,
      "type":"url",
      "status":"Active"
   }
]

调整后的Jolt转换规则

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "steps": {
          "*": "[]"
        }
      }
    }
  }
]

规则说明

  • *:遍历源数组中的每一个父对象
  • "steps":定位到父对象内的steps数组(无此数组的父对象会被自动跳过)
  • *:遍历steps数组中的每一个元素
  • "[]":将当前元素直接追加到根级数组中,Jolt会自动为元素分配连续的数组索引,不会产生null值或嵌套的索引键,最终生成完全扁平的目标数组。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.12 08:53:14