问题描述
如何使用Dataweave 2对需要按日期排序的json进行排序和排列
{
"things": [
{
"datetime": "2020-11-07T16:11:52.866Z","name": "foo"
},{
"datetime": "2020-11-07T16:11:39.971Z","name": "bar"
},{
"datetime": "2020-11-07T16:11:39.978Z","name": "baz"
}
]
}
解决方法
您可以使用orderBy
函数
%dw 2.0
output application/json
---
orderedDates: (payload.things orderBy $.datetime)
默认情况下,输出按升序排列。
如果需要下降,可以执行以下操作:
%dw 2.0
output application/json
---
orderedDates: (payload.things orderBy $.datetime)[-1 to 0]
如果您输入的时区不同,也可以使用日期时间格式。使用当前输入值,您可以使用LocalDateTime
%dw 2.0
output application/json
---
orderedDates: (payload.things orderBy ($.datetime as LocalDateTime {format: "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"}))
,
只需使用orderBy()并选择带有日期时间的字段即可。
%dw 2.0
output application/json
---
payload.things orderBy ((item,index) -> item.datetime)