您能否帮忙将以下输入数据转换为给定的输出:
输入:
[
{
"ID_TYPE": "4",
"DATE": "20230529",
"ADM_TIME": "17:00",
"PAYLOAD_NUMBER": "597418",
"PAYLOAD_COMMENT": "HELLO"
},
{
"ID_TYPE": "4",
"DATE": "20230531",
"ADM_TIME": "17:00",
"PAYLOAD_NUMBER": "597418",
"PAYLOAD_COMMENT": "HELLO"
},
{
"ID_TYPE": "4",
"DATE": "20230602",
"ADM_TIME": "17:00",
"PAYLOAD_NUMBER": "597418",
"PAYLOAD_COMMENT": "HELLO"
},
{
"ID_TYPE": "4",
"DATE": "20230628",
"ADM_TIME": "8:00",
"PAYLOAD_NUMBER": "597500",
"PAYLOAD_COMMENT": "Comments"
},
{
"ID_TYPE": "4",
"DATE": "20230628",
"ADM_TIME": "17:00",
"PAYLOAD_NUMBER": "597500",
"PAYLOAD_COMMENT": "Comments"
},
{
"ID_TYPE": "4",
"DATE": "20230629",
"ADM_TIME": "12:00",
"PAYLOAD_NUMBER": "597500",
"PAYLOAD_COMMENT": "Comments"
},
{
"ID_TYPE": "4",
"DATE": "20230630",
"ADM_TIME": "17:00",
"PAYLOAD_NUMBER": "597500",
"PAYLOAD_COMMENT": "Comments"
},
{
"ID_TYPE": "4",
"DATE": "20230702",
"ADM_TIME": "12:00",
"PAYLOAD_NUMBER": "597500",
"PAYLOAD_COMMENT": "Comments"
}
]
输出:
[
{
"TIMES": "17:00",
"MORNING": "0",
"NOON": "0",
"EVENING": "3",
"TOTAL_QUANTITY": "3",
"DATES": "2023/05/29, 2023/06/02, 2023/05/31",
"PAYLOAD_NUMBER": "597418",
"PAYLOAD_COMMENT": "HELLO"
},
{
"TIMES": "8:00, 12:00, 17:00",
"MORNING": "1",
"NOON": "2",
"EVENING": "2",
"TOTAL_QUANTITY": "5",
"DATES": "2023/06/28, 2023/06/29, 2023/06/30, 2023/07/02",
"PAYLAOD_NUMBER": "597500",
"PAYLOAD_COMMENT": "Comments"
}
]
如果输入是 groupby PAYLOAD_NUMBER 那么:
谢谢你。
您需要使用 groupBy() 然后 mapObject() 根据需要对每个组进行汇总、排序和格式化,然后获取结果列表。
%dw 2.0
output application/json
fun normalizeTime(t: String)= t as LocalTime {format: "H:mm"} as String {format: "HH:mm"}
fun normalizeDate(d: String)= d as Date {format: "yyyyMMdd"} as String {format: "yyyy/MM/dd"}
---
payload
groupBy ($.PAYLOAD_NUMBER)
mapObject ((value, key, index) -> (key): {
TIMES: value.*ADM_TIME distinctBy $ orderBy normalizeTime($) joinBy " ",
MORNING: sizeOf(value.ADM_TIME filter (normalizeTime($) >= "00:00" and normalizeTime($) <= "09:59" )),
NOON: sizeOf(value.ADM_TIME filter (normalizeTime($) >= "10:00" and normalizeTime($) <= "14:59" )),
EVENING: sizeOf(value.ADM_TIME filter (normalizeTime($) >= "15:00" and normalizeTime($) <= "23:59" )),
TOTAL_QUANTITY: sizeOf(value),
DATES: value.*DATE distinctBy $ orderBy $ map normalizeDate($) joinBy " ",
PAYLAOD_NUMBER: key as String,
PAYLOAD_COMMENT: value[0].PAYLOAD_COMMENT
})
pluck ($)
输出:
[
{
"TIMES": "17:00",
"MORNING": 0,
"NOON": 0,
"EVENING": 3,
"TOTAL_QUANTITY": 3,
"DATES": "2023/05/29 2023/05/31 2023/06/02",
"PAYLAOD_NUMBER": "597418",
"PAYLOAD_COMMENT": "HELLO"
},
{
"TIMES": "8:00 12:00 17:00",
"MORNING": 1,
"NOON": 2,
"EVENING": 2,
"TOTAL_QUANTITY": 5,
"DATES": "2023/06/28 2023/06/29 2023/06/30 2023/07/02",
"PAYLAOD_NUMBER": "597500",
"PAYLOAD_COMMENT": "Comments"
}
]
我添加了缺失的“00:00”时间。随意改变。它