如何在 Postman 的 json 文件中使用时间戳字段对 json 进行排序?
How do I sort a json using a time stamp field within the json file in Postman?
我是 Postman 的新手,我有一个 API 调用,它给我输出 json。在“测试”中,我通过 "bodyData = JSON.parse(responseBody);"
得到 JSON
JSON 文件有时间戳字段示例 "executedDate": "2020-11-26T09:45:27.000Z"
。我需要帮助根据 executedDate
字段对 JSON 文件进行降序排序。
{
"dataflowJobs": [
{
"createdDate": "2020-11-26T09:45:03.000Z",
"duration": 48,
"executedDate": "2020-11-26T09:45:27.000Z",
"id": "03C2w000002Urb1EAC",
"jobType": "user",
"label": "Dataflow_2",
"progress": 1.0,
"startDate": "2020-11-26T09:45:03.000Z",
"status": "Success",
"type": "dataflowjob",
"url": "/services/data/v47.0/wave/dataflowjobs/03C2w000002Urb1EAC"
},
{
"createdDate": "2020-11-26T09:45:01.000Z",
"duration": 34,
"executedDate": "2020-11-26T09:45:02.000Z",
"id": "03C2w000002UracEAC",
"jobType": "user",
"label": "Adv_Dataflow_Exercises",
"progress": 1.0,
"startDate": "2020-11-26T09:45:01.000Z",
"status": "Success",
"type": "dataflowjob",
"url": "/services/data/v47.0/wave/dataflowjobs/03C2w000002UracEAC"
}, {
"createdDate": "2020-11-20T09:45:01.000Z",
"duration": 58,
"id": "0eP2w000000MhszEAC",
"jobType": "recipe",
"label": "Feb_and_Jan_S2_Modified_Recipe2_recipe",
"progress": 1.0,
"startDate": "2020-11-20T09:45:01.000Z",
"status": "Success",
"type": "dataflowjob",
"url": "/services/data/v47.0/wave/dataflowjobs/0eP2w000000MhszEAC"
}
],
"url": "/services/data/v47.0/wave/dataflowjobs"
}
let moment = require('moment')
let jsonData = pm.response.json()
console.log(jsonData.dataflowJobs.sort(function (a, b) { return moment(b.executedDate).diff(moment(a.executedDate), "seconds") }))
这里会按照执行时间排序,没有执行时间排在最前面
我是 Postman 的新手,我有一个 API 调用,它给我输出 json。在“测试”中,我通过 "bodyData = JSON.parse(responseBody);"
JSON 文件有时间戳字段示例 "executedDate": "2020-11-26T09:45:27.000Z"
。我需要帮助根据 executedDate
字段对 JSON 文件进行降序排序。
{
"dataflowJobs": [
{
"createdDate": "2020-11-26T09:45:03.000Z",
"duration": 48,
"executedDate": "2020-11-26T09:45:27.000Z",
"id": "03C2w000002Urb1EAC",
"jobType": "user",
"label": "Dataflow_2",
"progress": 1.0,
"startDate": "2020-11-26T09:45:03.000Z",
"status": "Success",
"type": "dataflowjob",
"url": "/services/data/v47.0/wave/dataflowjobs/03C2w000002Urb1EAC"
},
{
"createdDate": "2020-11-26T09:45:01.000Z",
"duration": 34,
"executedDate": "2020-11-26T09:45:02.000Z",
"id": "03C2w000002UracEAC",
"jobType": "user",
"label": "Adv_Dataflow_Exercises",
"progress": 1.0,
"startDate": "2020-11-26T09:45:01.000Z",
"status": "Success",
"type": "dataflowjob",
"url": "/services/data/v47.0/wave/dataflowjobs/03C2w000002UracEAC"
}, {
"createdDate": "2020-11-20T09:45:01.000Z",
"duration": 58,
"id": "0eP2w000000MhszEAC",
"jobType": "recipe",
"label": "Feb_and_Jan_S2_Modified_Recipe2_recipe",
"progress": 1.0,
"startDate": "2020-11-20T09:45:01.000Z",
"status": "Success",
"type": "dataflowjob",
"url": "/services/data/v47.0/wave/dataflowjobs/0eP2w000000MhszEAC"
}
],
"url": "/services/data/v47.0/wave/dataflowjobs"
}
let moment = require('moment')
let jsonData = pm.response.json()
console.log(jsonData.dataflowJobs.sort(function (a, b) { return moment(b.executedDate).diff(moment(a.executedDate), "seconds") }))
这里会按照执行时间排序,没有执行时间排在最前面