Filter response to only include some fields and put in a flow!

I am trying to automate some awkward tasks for work. One of which is query multiple endpoints to get the backup status of an appliance, parse the body response so I just see a datestamp + success/failure result.
Iā€™d like this to work in a 1-click flow but Iā€™m not sure how to compose this in flows.

Below is an example response, Iā€™d like to do the following:
1/ Convert the milisecond timestamp to a DD/MM/YYYY format (or similar, something readable)
2/ Only show me the ā€˜Cluster_backup_statusā€™ fields.

`{
    "backup_config": {
        "backup_enabled": true,
        "backup_schedule": {
            "seconds_between_backups": 86400,
            "resource_type": "IntervalBackupSchedule"
        },
        "inventory_summary_interval": 240,
        "remote_file_server": {
            "server": "xx.xx.xx",
            "port": 22,
            "protocol": {
                "protocol_name": "sftp",
                "ssh_fingerprint": ",
                "authentication_scheme": {
                    "scheme_name": "PASSWORD",
                    "username": "nsx-backup"
                }
            },
            "directory_path": "/nsx/backup"
        }
    },
    "current_backup_operation_status": {
        "operation_type": "none"
    },
    "backup_operation_history": {
        "cluster_backup_statuses": [
            {
                "backup_id": "7707cfd4-0edd-4113-8fca-599b5388e96e-1660828721",
                "start_time": 1660828721017,
                "end_time": 1660828787026,
                "success": true
            }
        ],
        "node_backup_statuses": [
            {
                "backup_id": "7707cfd4-0edd-4113-8fca-599b5388e96e-1660828721",
                "start_time": 1660828721017,
                "end_time": 1660828796250,
                "success": true
            }
        ],
        "inventory_backup_statuses": [
            {
                "backup_id": "inventory-1660833041",
                "start_time": 1660833041013,
                "end_time": 1660833041233,
                "success": true
            }
        ]`

Can anyone help? I believe I need to write a ā€˜Testā€™ to do this, but how can I convert the date/timestamp and output the desired results into a console.

@davebaker87

// Store sample response.

const response = [
    {   "backup_config": {
        "backup_enabled": true,
        "backup_schedule": {
            "seconds_between_backups": 86400,
            "resource_type": "IntervalBackupSchedule"
        },
        "inventory_summary_interval": 240,
        "remote_file_server": {
            "server": "xx.xx.xx",
            "port": 22,
            "protocol": {
                "protocol_name": "sftp",
                "ssh_fingerprint": "",
                "authentication_scheme": {
                    "scheme_name": "PASSWORD",
                    "username": "nsx-backup"
                }
            },
            "directory_path": "/nsx/backup"
        }
    },
    "current_backup_operation_status": {
        "operation_type": "none"
    },
    "backup_operation_history": {
        "cluster_backup_statuses": [
            {
                "backup_id": "7707cfd4-0edd-4113-8fca-599b5388e96e-1660828721",
                "start_time": 1660828721017,
                "end_time": 1660828787026,
                "success": true
            }
        ],
        "node_backup_statuses": [
            {
                "backup_id": "7707cfd4-0edd-4113-8fca-599b5388e96e-1660828721",
                "start_time": 1660828721017,
                "end_time": 1660828796250,
                "success": true
            }
        ],
        "inventory_backup_statuses": [
            {
                "backup_id": "inventory-1660833041",
                "start_time": 1660833041013,
                "end_time": 1660833041233,
                "success": true
            }
        ]
    }
    }];


// console.log(response);

// retrieve just the cluster_backup_statuses
var clusterBackupStatus = response[0].backup_operation_history.cluster_backup_statuses.map(cluster_backup_statuses => cluster_backup_statuses);

// Update the dates
clusterBackupStatus[0].start_time = Date(clusterBackupStatus[0].start_time);
clusterBackupStatus[0].end_time = Date(clusterBackupStatus[0].end_time);
// Write updated array to console
console.log(clusterBackupStatus);

// another method for getting the cluster_backup_statuses
// for this example, I'm just returning the backup_id, success and start_time.
var clusterBackupStatusV2 = response[0].backup_operation_history.cluster_backup_statuses.map(({ backup_id, success, start_time }) => ({ backup_id, success, start_time }));;
// Update the date
clusterBackupStatusV2[0].start_time = Date(clusterBackupStatusV2[0].start_time);
// Write updated array to console 
console.log(clusterBackupStatusV2);
1 Like

The console looks like this.

image

The following will tell you how to format dates to your liking.

1 Like

@davebaker87

I took @michaelderekjones great example and tweaked it to use the find function and moment to format the dates nicely. Then added error handling in case there were no cluster backups in the response.

This is the beauty of code, there are so many ways of achieving the same result. I prefer the moment library as I find it easier to work with although itā€™s not really maintained anymore, while itā€™s built into Postman Iā€™ll continue to use it.

const moment = require('moment')

// retrieve just the cluster_backup_statuses
const results = response.find(r => r.backup_operation_history.cluster_backup_statuses)
const clusterBackupStatus = results ? results.backup_operation_history.cluster_backup_statuses.map(cluster_backup_statuses => cluster_backup_statuses) : undefined

// Update the dates
if (clusterBackupStatus) {
    clusterBackupStatus[0].start_time = moment(clusterBackupStatus[0].start_time).format('DD/MM/YYYY');
    clusterBackupStatus[0].end_time = moment(clusterBackupStatus[0].end_time).format('DD/MM/YYYY');
    // Write updated array to console
    console.log(clusterBackupStatus);
} else {
    console.log('No cluster backups found')
}

Console Output:

(1) [{ā€¦}]
0: {ā€¦}
backup_id: "7707cfd4-0edd-4113-8fca-599b5388e96e-1660828721"
start_time: "18/08/2022"
end_time: "18/08/2022"
success: true

@cruxto

No problem. The more ways of achieving something the better. Iā€™ve only really just started using Postman, coming across from SoapUI.

I need to go back and re-learn Javascript, I know enough to read\edit and understand most examples, but could do with learning the advanced features\methods so I know what is available.

1 Like

Iā€™m a bit confused - sorry for such newb questions (and this may also evidence my misunderstanding of Postmanā€™s use) - but I had expected to need to write this code in the ā€˜Testsā€™ section so itā€™s ran after the response is received. What does the below do? And where should this be entered in the UI in postman?

const response = [
body text

console.log(response);

As I donā€™t have direct access to your response, I created a variable that included your sample, for testing the Javascript functions against.

All of this code is in the test tab in PostMan, but I havenā€™t written any tests per se at this point. Iā€™ve just written to the console log.

Usually I would parse the response using the following command instead if defining a new object.

Something likeā€¦

const response = pm.response.json();

This is all covered in the Postman Training. Go to the Postman main page. Select the ā€œLearning Centerā€ down the left hand side. On the next page select Postman training, and I would recommend the ā€œGalaxy APIā€™s 101ā€ and the ā€œGalaxy Testing and Automationā€ as the starting point. It will show you how to parse the response and write tests.

Postman uses Javascript so it allows you to use a lot of native Javascript functions for manipulation of responses. Searches, date manipulation, etc. So you probably want to take a basic javascript course at some point as well to get the most out of Postman. The course on WC3 schools is a good starting point.

image

Well, you are doing a great job @michaelderekjones. Iā€™m in a similar situation, migrating a load of test packs from SoapUI / Ready API to Postman.