1

如何使用 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"
  }
 ]
}
4

2 回答 2

2

您可以使用该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'"}))
于 2020-11-09T17:36:34.050 回答
1

只需使用 orderBy() 并选择带有日期时间的字段。

%dw 2.0
output application/json
---
payload.things orderBy ((item, index) -> item.datetime)
于 2020-11-09T17:31:05.767 回答