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

如何在MuleSoft中按条件移除嵌套数组的banNum属性

MuleSoft DataWeave实现指定条件移除JSON属性

在MuleSoft中,你可以使用DataWeave 2.0来实现这个需求,通过遍历JSON结构并根据指定条件修改属性。以下是具体的实现脚本:

%dw 2.0
output application/json
---
inventory: payload.inventory map (inv) -> {
    category: {
        name: inv.category.name,
        services: inv.category.services map (svc) -> 
            if (svc.category == "SL" and svc.available == true) {
                (svc - "accountDetails") ++ {
                    accountDetails: svc.accountDetails map (acc) -> acc - "banNum"
                } if (svc.accountDetails != null) else svc
            } else svc
    }
}

脚本说明:

  • 遍历inventory数组:用map函数逐个处理每个inventory对象
  • 处理services数组:同样用map遍历每个service,判断是否满足category为SL且available为true的条件
  • 移除banNum属性:当条件满足时,先移除原service的accountDetails字段,再重新生成一个去掉banNum的accountDetails数组;如果service没有accountDetails则直接返回原对象
  • 非匹配项保留原样:不满足条件的service直接返回原始结构,避免修改其他数据

原始Payload

{
  "inventory": [
   {
    "category": {
     "name": "inventory1",
     "services": [
      {
       "category": "FB",
       "serviceType": "FB",
       "available": false,
       "accountDetails": [
        {
         "banNum": "1111",
         "status": "A"
        }
       ]
      },
      {
       "category": "SL",
       "serviceType": "SL",
       "available": true,
       "accountDetails": [
        {
         "banNum": "12345",
         "status": "A",
         "inventor": true,
         "migration": false,
         "indicator": true
        }
       ]
      }
     ]
    }
   },
   {
    "category": {
     "name": "inventory2",
     "services": [
      {
       "category": "wireless",
       "desc": "wireless",
       "available": true
      }
     ]
    }
   }
  ]
}

预期Payload

{
  "inventory": [
   {
    "category": {
     "name": "inventory1",
     "services": [
      {
       "category": "FB",
       "serviceType": "FB",
       "available": false,
       "accountDetails": [
        {
         "banNum": "1111",
         "status": "A"
        }
       ]
      },
      {
       "category": "SL",
       "serviceType": "SL",
       "available": true,
       "accountDetails": [
        {
         "status": "A",
         "inventor": true,
         "migration": false,
         "indicator": true
        }
       ]
      }
     ]
    }
   },
   {
    "category": {
     "name": "inventory2",
     "services": [
      {
       "category": "wireless",
       "desc": "wireless",
       "available": true
      }
     ]
    }
   }
  ]
}

使用方式

将上述DataWeave脚本放入Mule Flow的Transform Message组件中,组件的输入连接到原始JSON载荷的来源(比如HTTP请求、文件读取等),输出即为处理后的预期Payload。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.11 09:31:05