如何在DataWeave Mule esb

时间:2016-10-07 18:51:35

标签: mule mule-studio mule-component mule-el dataweave

我正致力于改造我的有效载荷。我有这种情况。

输入有效负载如下所示: -

{
 "address": {
    "city": "bab",
    "company_name": "asdast",
    "country_code": "sam",
    "location": {
    "city": null,
    "state": null
  }
}}

我使用%output application/json skipNullOn = "everywhere"它会返回JSON,如下所示

{
 "address": {
"city": "bab",
"company_name": "asdast",
"country_code": "sam",
"location": { }
}}

但如果位置对象中的所有字段都为空,我不想要空位置对象。我期待这样的事情

{   
"address": {
"city": "bab",
"company_name": "asdast",
"country_code": "sam"
}}

7 个答案:

答案 0 :(得分:6)

这是我在直接方法似乎难以理解后得出的递归解决方案:

%dw 1.0
%output application/json

%function acceptable(value) (
    (value default {}) != {}
)

%function filterKeyValue(key, value) (
    ((key): value) when acceptable(value)
)

%function removeFields(o) o
    unless o is :object
    otherwise o mapObject
        (filterKeyValue($$, removeFields($)))

---
removeFields(payload)

这是我开始的直接方法:

%dw 1.0
%output application/json

%function skipNulls(o) o 
    unless o is :object 
    otherwise o mapObject {
        (($$): $) when ($ != null)
    }

%function skipEmpty(o) o mapObject {
        (($$): $) when ($ != {})
    }

---
address: skipEmpty(payload.address
    mapObject { 
        ($$): skipNulls($)
    }
)

请注意,我们在skipNullOn="everywhere"指令上删除了%output,并删除了函数中的空字段。这允许我们确保在之前删除空值我们检查包含的对象是否为空。

函数(在两个解决方案中)都有效,因为mapObject允许我们循环遍历对象字段,并且只有当它们满足某个条件时才将它们包含在结果对象中。

答案 1 :(得分:2)

这对我有用(N.B. Dataweave来自Mule版本3.8):

%function isEmpty(thing) thing match {
  :null -> true,
  arr is :array -> arr == [],
  obj is :object -> obj == {},
  '' -> true,
  /\s+/ -> true,
  default -> false
}

更新:

所以,要在Ryan上面的解决方案中注入这个:

%function acceptable(value) (
    !isEmpty(value)
)

答案 2 :(得分:2)

Ryan,该功能在Studio 6.2.3中产生错误。我不得不包括另外一个条件。我必须在对象构造函数花括号中包围(key):value,并且我必须包含其他条件:

%function filterKeyValue(key, value) 
(
  //((key): value) when acceptable(value)
  {(key) : value} when acceptable(value)
  otherwise {}
)

答案 3 :(得分:1)

不幸的是,没有一个解决方案对我有用,所以我使用了第二个“转换消息”组件和下面的代码,并在两个组件中使用了skipNullOn =“everywhere”。此代码以递归方式搜索空元素(空字段,空字符串,空数组和空对象)并将其删除。

%dw 1.0
%function removeEmptyInArray(arr) arr map (
    (removeEmptyInArray($) when $ is :array 
    otherwise (removeEmptyInObject($) when $ is :object
    otherwise $ when ($ != null and (sizeOf $) > 0) otherwise null))
) when arr != [] 
otherwise null
%function removeEmptyInObject(obj) obj mapObject (
    '$$': (removeEmptyInArray($) when $ is :array 
    otherwise (removeEmptyInObject($) when $ is :object
    otherwise $ when ($ != null and (sizeOf $) > 0) otherwise null))
)

%output application/json skipNullOn="everywhere"
---

removeEmptyInObject(payload)

希望它有所帮助。

答案 4 :(得分:1)

我有最简单,最简单的解决方案。

%dw 1.0
%output application/json skipNullOn = "everywhere"
---
{
    "address": {
    "city": payload.address.city,
    "company_name": payload.address.company_name,
    "country_code": payload.address.country_code,
    ("location": {
        "city": payload.address.location.city,
        "state": payload.address.location.state
  })
  } 
}

答案 5 :(得分:0)

没有直接的方法可以做到这一点,你可以做这样的事情

%dw 1.0
%output application/json
--- 
address:  payload.address - "location" when (sizeOf (payload.address.location pluck $ filter $ != null)) == 0 otherwise payload

希望这有帮助。

答案 6 :(得分:0)

使用此功能

%function acceptable(value) (
    !isEmpty(value)
)