如何在Dataweave中调试MAP函数?

时间:2018-08-13 13:37:51

标签: json mule dataweave

我从源头收到以下json:

{
    "totalRows": 2,
    "startRow": 1,
    "endRow": 2,
    "hasNextPage": false,
    "searchResults": {
        "603": {
            "dDocOwner": "sysadmin",
            "dIndexedID": "603",
            "dDocAuthor": "sysadmin",
            "dDocCreator": "sysadmin"
        },
        "3011": {
            "dDocOwner": "sysadmin",
            "dIndexedID": "3011",
            "dDocAuthor": "sysadmin",
            "dDocCreator": "sysadmin"
        }
    }
}

我想将以上请求转换为目标特定请求,如下所示:

{
"data" : [
{
   "ID":"603",
   "Creator":"6141",
   "Author":"sysadmin",
   "Owner":"sysadmin"},
{
   "ID":"3011",
   "Creator":"6141",
   "Author":"sysadmin",
   "Owner":"sysadmin"}
   ]
}

我已经在Dataweave中创建了以下映射,但是它不起作用,能否请您给我一些相同的指针:

%dw 1.0
%output application/json skipNullOn="objects"
---
{
    "data": (payload.searchResults.* map ((payload01 , indexOfPayload01) ->{
	"id": payload01.dIndexedID,
   "Creator": payload01.dDocCreator,
   "Author": payload01.dDocName,
   "Owner": payload01.dDocOwner,
}) when (sizeOf payload) >= 1  otherwise [])
}

1 个答案:

答案 0 :(得分:0)

您正在将对象映射到数组,pluck非常适合!

%dw 1.0
%output application/json
---
{
  data: payload.searchResults pluck ((docObject) -> {
    ID:      docObject.dDocIndexedID,
    Creator: docObject.dDocCreator,
    Author:  docObject.dDocAuthor,
    Owner:   docObject.dDocOwner
  })
}