How too fetch name of array

Hi,
I am working on automation script using postman , I have received response as below
{
“1844”: {
“totalExecutions”: 43,
“projectKey”: “P84”,
“endDate”: “31/Jan
"executionSummaries”: {
“executionSummary”: []
}
},
“1845”: {
“totalExecutions”: 19,
“projectKey”: “P98”,
“endDate”: “31/Jan/18”,
}
},
"-1": {
“totalExecutions”: 0,
“projectKey”: “P234”,
“endDate”: “”,
“executionSummaries”: {
“executionSummary”: []
}
},
“recordsCount”: 3
}
I want to iterate through each array , get array name (1844,1845,-1) , and Go inside each array get the Project key , After getting project key I need to compare with value I am passing return array name (1844,1845,-1) of matched project key .
But I am not able to iterate through each array , when i tried to get data.length its returning me null value .
I am not able to get name of each array also
Would you please help me how to get name of each array and how to iterate into each array.
Count no of arrays by script instead of using recordsCount": 3

Preeti.

Hey @Prit, The response you have mentioned is not valid JSON, I have fixed the JSON and created a mock server for you to test things. A GET request to this endpoint should respond with the sanitized JSON data for you.

Now the next part is pretty straightforward, parse the response as JSON and loop through the keys to populate the values you need, something like

var jsonData = pm.response.json();
var projectKeys = [],
    dataKeys = [];
for (var i in jsonData) {
    if(jsonData[i].hasOwnProperty('projectKey')) {
        dataKeys.push(i);
        projectKeys.push(jsonData[i].projectKey);
    }
}

Now, dataKeys will have the array name values that you need(1844,1845,-1) and projectKeys will have the projectKey for each data item(P84, P98, P234).

I couldn’t understand the part where you wanted to compare these to something but this should help you get started. Feel free to reach out here in case you get stuck over something or need additional help.

Hi , Thank you very much for your response

Whole response I am getting as

{

"16711": [

    {

        "1844": {

            "totalExecutions": 43,

            "endDate": "31/Jan/18",

            "description": "Test cycle for Functional test cases created for PCI testing on DPC",

            "totalExecuted": 42,

            "started": "true",

            "versionName": "PCI Compliance for DPC",

            "expand": "executionSummaries",

            "projectKey": "PCFD",

            "versionId": 16711,

            "environment": "GRE01",

            "totalCycleExecutions": 43,

            "totalDefects": 2,

            "build": "PCI 4.0",

            "createdBy": "a682346",

            "ended": "true",

            "name": "Functional PCI testing on DPC",

            "totalFolders": 0,

            "modifiedBy": "a682346",

            "projectId": 17701,

            "createdByDisplay": "Bate, Pravin",

            "startDate": "21/Dec/17",

            "executionSummaries": {

                "executionSummary": []

            }

        },

        "1845": {

            "totalExecutions": 19,

            "endDate": "31/Jan/18",

            "description": "",

            "totalExecuted": 19,

            "started": "true",

            "versionName": "PCI Compliance for DPC",

            "expand": "executionSummaries",

            "projectKey": "PCFD",

            "versionId": 16711,

            "environment": "GRE01",

            "totalCycleExecutions": 19,

            "totalDefects": 8,

            "build": "PCI 4.0",

            "createdBy": "a665679",

            "ended": "true",

            "name": "Non- Functional Security Testing",

            "totalFolders": 0,

            "modifiedBy": "a682346",

            "projectId": 17701,

            "createdByDisplay": "Chowdhury, Barnavo",

            "startDate": "5/Jan/18",

            "executionSummaries": {

                "executionSummary": []

            }

        },

        "1852": {

            "totalExecutions": 9,

            "endDate": "31/Jan/18",

            "description": "Regression Testing",

            "totalExecuted": 9,

            "started": "true",

            "versionName": "PCI Compliance for DPC",

            "expand": "executionSummaries",

            "projectKey": "PCFD",

            "versionId": 16711,

            "environment": "GRE01",

            "totalCycleExecutions": 9,

            "totalDefects": 6,

            "build": "PCI 4.0",

            "createdBy": "a612856",

            "ended": "true",

            "name": "Regression_Testing",

            "totalFolders": 0,

            "modifiedBy": "a682346",

            "projectId": 17701,

            "createdByDisplay": "Dhiraj Shah",

            "startDate": "22/Jan/18",

            "executionSummaries": {

                "executionSummary": []

            }

        },

        "-1": {

            "totalExecutions": 0,

            "endDate": "",

            "description": "",

            "totalExecuted": 0,

            "started": "",

            "versionName": "PCI Compliance for DPC",

            "expand": "executionSummaries",

            "projectKey": "PCFD",

            "versionId": 16711,

            "environment": "",

            "totalCycleExecutions": 0,

            "build": "",

            "ended": "",

            "name": "Ad hoc",

            "modifiedBy": "",

            "projectId": 17701,

            "startDate": "",

            "executionSummaries": {

                "executionSummary": []

            }

        },

        "recordsCount": 4

    }

],

"-1": [

    {

        "-1": {

            "totalExecutions": 6,

            "endDate": "",

            "description": "",

            "totalExecuted": 0,

            "started": "",

            "versionName": "Unscheduled",

            "expand": "executionSummaries",

            "projectKey": "PCFD",

            "versionId": -1,

            "environment": "",

            "totalCycleExecutions": 6,

            "build": "",

            "ended": "",

            "name": "Ad hoc",

            "modifiedBy": "",

            "projectId": 17701,

            "startDate": "",

            "executionSummaries": {

                "executionSummary": []

            }

        },

        "recordsCount": 1

    }

]

}

I want to know How find length of Array(16771& -1 , now I m getting these 2 main array ) and Subarray( 1844,1845,1852,-1& -1 ) ?

They have given “recordsCount” parameter for subarrays we can use that to find length of subarray ?

I am passing project key as global variable from different api req and comaprining with given json response , In which array that global project key matches I want to get that array name and set it as global variable , that array name I will use in next api request .

How to do this ?

Preeti.

Hey @Prit. The recordsCount variable here can be used to determine what you’re looking for, it is not exactly the array length btw since the array has a single item which is an object, it is basically the number of key-value pairs present in that object to be more precise.

Now let’s get to your use case and see what exactly are we trying to do. We want to

  • Find the number of key-value pairs in each object present in the array, indicated by the recordsCount variable

  • We want to find out which all items have the projectKey parameter and collect the keys of the ones that match the global project key extracted from the global variables.

  • Finally, we want to set the ones that match as a global variable to be accessed in the next request.

If I understand these requirements correctly, then here’s how you can do it. Please correct me if I am wrong and I’ll update the answer accordingly.

var jsonData = pm.response.json();
var projectKeys = {},
    recordCounts = {},
    hasProjectKey = [],
    // get the global key from the global variables
    globalKey = pm.globals.get("projectKey");

for (var i in jsonData) {
    jsonData[i].forEach(function(item) {
        for (var key in item) {
            if(key == 'recordsCount') {
                // for each object, pick the recordsCount. Populate a new 
                // object with the original key name as the key and
                // recordsCount as value 
                recordCounts[i] = item[key]
            }
            else {
                if (item[key].hasOwnProperty('projectKey') && 
                    item[key].projectKey == globalKey) {
                    // check if the object has projectKey property and if 
                   // it matches the global key push the key name to an
                   //  array if it does.
                    hasProjectKey.push(key);
                    projectKeys[key] = item[key].projectKey;
                }
            }
        }
    })
}
console.log(projectKeys);
// set a global variables with the keys to be used in next request.
pm.globals.set('matchesProjectKey', hasProjectKey);

This is not the most optimized way to do what you need and can be improved upon, which you should try to do. I would recommend you to log out the different variables in the loop to see what exactly is going on if you have any confusions.

The global variable, after execution, would be 1844,1845,1852,-1,-1. The projectKeys variable has the actual keys. It looks like:

{
  1844:"PCFD",
  1845:"PCFD",
  1852:"PCFD",
  -1:"PCFD"
}

Hope this helps. Feel free to reach out with any additional queries. Cheers.

1 Like