简体   繁体   English

如何使用 Postman 文件中的 json 文件中的时间戳字段对 json 进行排序?

[英]How do I sort a json using a time stamp field within the json file in Postman?

I am new to Postman, I have a API call which gives me the output as a json.我是 Postman 的新手,我有一个 API 呼叫,它给了我 output 作为 Z466DEEC76ECDF5FCA6DZ5857.FZ。 In the "Test", I get the JSON by "bodyData = JSON.parse(responseBody);"在“测试”中,我通过"bodyData = JSON.parse(responseBody);"得到 JSON;

The JSON file has got timestamp fields example "executedDate": "2020-11-26T09:45:27.000Z" . JSON 文件具有时间戳字段示例"executedDate": "2020-11-26T09:45:27.000Z" I would need help in sorting the JSON file in descending order based on the executedDate field.在根据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") }))

This will sort according to executed time, if there is no executed time it will be at the top这将根据执行时间排序,如果没有执行时间它将在顶部

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM