Mule Dataweave无法强制a:null到a:number

时间:2017-11-01 15:46:41

标签: java null dataweave

我正在构建两个不同Json数据流的Java映射。但是,如果其中一个流在数量中包含空值,则流将在以下条件下出错:

 ($.itemid) : '"' ++ skuLookup[$.sku][0].qty as :number as :string {format: "#.0000"} ++ '"'
                                                                         ^
 Cannot coerce a :null to a :number 
 (com.mulesoft.weave.mule.exception.WeaveExecutionException). Message payload is of type: String

理想的情况是忽略迭代中的空值。这里的数据编织是未触及的:

  %output application/java
  %var skuLookup =  flowVars.SSRCreateStarshipItems groupBy $.sku
  ---
  flowVars.SSRGetOrderItems map ({
  ($.itemid) : '"' ++ skuLookup[$.sku][0].qty as :number as :string 
  {format: "#.0000"} ++ '"'
  })

这是我理想的解决方案,但我似乎无法让它发挥作用:

  %output application/java
  %var skuLookup =  flowVars.SSRCreateStarshipItems groupBy $.sku
  ---
  flowVars.SSRGetOrderItems map ({
  ($.itemid) : '"' ++ skuLookup[$.sku][0].qty as :number as :string 
  {format: "#.0000"} ++ '"'
  }) when skuLookup[$.sku][0].qty != null

1 个答案:

答案 0 :(得分:0)

设置默认值可解决您的问题。 例如,如果您传入的负载如下:

{
    "testMap": [
        {
            "testName": "TestName",
            "Value": "3"
        },
        {
            "testName": "TestName",
            "Value": null
        }
    ]
}

您可以将Value键转换为数字或使用以下方法传递null:

%dw 1.0
%input payload application/json
%output application/json
---
payload.testMap map (data, index) -> {

  testName: data.testName,
  Value: data.Value as :number as :string {format: "#.0000"} default null
}

这就是结果:

[
  {
    "testName": "TestName",
    "Value": "3.0000"
  },
  {
    "testName": "TestName",
    "Value": null
  }
]

此外,如果要完全删除密钥,请在json:

的输出标头后插入
skipNullOn="everywhere"