使用内部数组将json数组转换为单个json

时间:2019-01-03 21:51:31

标签: mule dataweave

我从json格式的系统中收到一条消息,并希望执行转换以获取以下消息

源消息:

    [
{
    "OPERATING_COMPANY": "xx",
    "STORE_CLOSING_TIME": "2019-01-03T22:00:00",
    "BATCH_ID": 1812,
    "STORENUMBER": 1197,
    "PROCESS_STATUS": "P"
},
{
   "OPERATING_COMPANY": "xx",
    "STORE_CLOSING_TIME": "2019-01-04T18:00:00",
    "BATCH_ID": 1812,
    "STORENUMBER": 1197,
    "PROCESS_STATUS": "P"
},
{
    "OPERATING_COMPANY": "xx",
    "STORE_CLOSING_TIME": "2019-01-03T22:00:00",
    "BATCH_ID": 1314,
    "STORENUMBER": 1198,
    "PROCESS_STATUS": "P"
},
{
    "OPERATING_COMPANY": "xx",
    "STORE_CLOSING_TIME": "2019-01-04T18:00:00",
    "BATCH_ID": 1314,
    "STORENUMBER": 1198,
    "PROCESS_STATUS": "P"
}]

预期产量

    [
{
    "OPERATING_COMPANY": "xx",
    "STORE_CLOSING_TIME": ["2019-01-03T22:00:00","2019-01-04T18:00:00"],
    "BATCH_ID": 1812,
    "STORENUMBER": 1197,
    "PROCESS_STATUS": "P"
},
{
   "OPERATING_COMPANY": "xx",
    "STORE_CLOSING_TIME": ["2019-01-03T22:00:00","2019-01-04T18:00:00"],
    "BATCH_ID": 1812,
    "STORENUMBER": 1198,
    "PROCESS_STATUS": "P"
}]

我是dataweave的新手,曾尝试使用map,但无法理解如何获得结果。

1 个答案:

答案 0 :(得分:0)

在dataweave中尝试以下操作:

%dw 1.0
%output application/json
%var time=(payload map $.STORE_CLOSING_TIME)  distinctBy $.STORE_CLOSING_TIME 
---
(payload map {

 "OPERATING_COMPANY": $.OPERATING_COMPANY,
 "STORE_CLOSING_TIME": time,
 "BATCH_ID": $.BATCH_ID,
 "STORENUMBER": $.STORENUMBER,
 "PROCESS_STATUS": $.PROCESS_STATUS 
    }) distinctBy $.STORENUMBER 

输出:

[
    {
        "OPERATING_COMPANY": "xx",
        "STORE_CLOSING_TIME": [
            "2019-01-03T22:00:00",
            "2019-01-04T18:00:00"
        ],
        "BATCH_ID": 1812,
        "STORENUMBER": 1197,
        "PROCESS_STATUS": "P"
    },
    {
        "OPERATING_COMPANY": "xx",
        "STORE_CLOSING_TIME": [
            "2019-01-03T22:00:00",
            "2019-01-04T18:00:00"
        ],
        "BATCH_ID": 1314,
        "STORENUMBER": 1198,
        "PROCESS_STATUS": "P"
    }
]