在循环数据编织中过滤相同级别的对象值 - Mule 3.7

时间:2017-03-21 14:19:50

标签: mule dataweave

我试图在Dataweave中的有效负载中过滤相同级别的对象值。我能够循环但它不会产生预期的输出。 样本有效负载:

{
    "root": {
        "createItem": {
            "itemInfo": {
                "lines": [{
                    "lineIdentifier": "4",
                    "Attributes": "Test1",
                    "partNumber": "QZRB"
                }, {
                    "lineIdentifier": "10",
                    "Attributes": "Test3",
                    "partNumber": "QPR1"
                }, {
                    "lineIdentifier": "12",
                    "Attributes": "Test4",
                    "partNumber": "QHT2"
                }]
            }
        },
        "ItemResponse": {
            "lines": [{
                "lineIdentifier": 4,
                "itemName": "QZRB",
                "status": "FAILED"
            }, {
                "lineIdentifier": 10,
                "itemName": "QPR1",
                "status": "COMPLETE"
            }, {
                "lineIdentifier": 12,
                "itemName": "QHT2",
                "status": "COMPLETE"
            }]
        }
    }
}

预期产出:

{
    "root": {
        "createItem": {
            "itemInfo": {
                "lines": [ {
                    "lineIdentifier": "10",
                    "Attributes": "Test3",
                    "partNumber": "QPR1"
                }, {
                    "lineIdentifier": "12",
                    "Attributes": "Test4",
                    "partNumber": "QHT2"
                }]
            }
        }
    }
}

这就是我在做的事情:

{
    root: {
        (payload.root.createItem.itemInfo.lines map ((respLines, indexOfRespLines) -> {
        items:payload.root.ItemResponse.lines filter ($.itemName == respLines.partNumber and $.status =='COMPLETE') map 
        {
            item: $.itemName,
         attributes: respLines.Attributes
         }

        }

        )
        )
    }
}

我如何实现这一目标?

谢谢, ROA

1 个答案:

答案 0 :(得分:0)

试试这个:

%dw 1.0
%output application/json
%var completedLines = payload.root.ItemResponse.lines filter $.status == 'COMPLETE' map $.lineIdentifier as :string
---
{
    root: {
        createItem: {
            itemInfo: {
                lines: payload.root.createItem.itemInfo.lines filter (completedLines contains $.lineIdentifier)
            }
        }
    }
}

请注意as :string中的completedLines,因为lineIdentifier中的ItemResponse是一个数字,而itemInfo中的{{1}}是一个字符串。