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 @mdjones 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 @mdjones. I’m in a similar situation, migrating a load of test packs from SoapUI / Ready API to Postman.