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

Mule 3中如何用DataWeave 1.0映射数组节点获取指定字段

解决DataWeave 1.0提取offers.offer.productOffers.productOffer中uri和id返回null的问题

问题分析

你的示例负载里,部分offer对象的productOffers下没有productOffer数组(比如第一个offer仅包含productOffers.uri)。直接用map操作时,访问不存在的productOffers.productOffer会返回null,导致结果不符合预期。

解决方案

使用默认值处理缺失字段结合flatten合并结果,确保仅提取有效数据:

%dw 1.0
%output application/json
---
flatten(payload.offers.offer map (offer) -> 
    (offer.productOffers.productOffer default []) map (po) -> {
        uri: po.uri,
        id: po.id
    }
)

代码说明

  • offer.productOffers.productOffer default []:当productOffer不存在时,用空数组替代null,避免map操作报错。
  • flatten:将多个productOffer数组的结果合并为一个扁平化列表,方便后续使用。
  • 最终只保留存在productOffer条目里的uri和id字段。

测试结果

针对你提供的示例负载,上述代码输出:

[
  {
    "uri": "https",
    "id": 9877897987
  }
]

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.11 20:01:13