Use of json output (array) into new requests

Hi all

New to postman and the use of json (arrays).

My question:

I want to use the json array output of a GET call and use this into a new GET request. The second output should be used into a POST call. In detail:

1st GET call:
The first call will result in a json array of id’s.

Example:

{
“values”: [
{
“id”: “d935b234-96dc-4ac0-b1b7-cde69d0bab75”,
“name”: “Test1”
},
{
“id”: “5ec38828-ab5c-4a86-97e3-c048f82aad6b”,
“name”: “Test2”
},
{
“id”: “38d1266a-3e2f-4c27-b605-1c29a42c0298”,
“name”: “Test3”
}
]
}

2nd GET call:
With the ouput of the first call I would like to loop over the id’s and use them into the 2nd call. This call would get the properties for each id. The output of this call should contain the info from the field { “name” , so remove all prior to the name and POST this into the 3rd call.

Example:

{
“metadata”: {
“configurationVersions”: [
7
],
“clusterVersion”: “1.222.86.20210802-200423”
},
“id”: “d935b234-96dc-4ac0-b1b7-cde69d0bab75”,
“name”: “Test1”,
“description”: null,
“rules”: [
{
“type”: “SERVICE”,
“enabled”: true,
“valueFormat”: null,
“propagationTypes”: [
“SERVICE_TO_PROCESS_GROUP_LIKE”
],
“conditions”: [
{
“key”: {
“attribute”: “PROCESS_GROUP_NAME”,
“type”: “STATIC”
},
“comparisonInfo”: {
“type”: “STRING”,
“operator”: “CONTAINS”,
“value”: “mendix”,
“negate”: false,
“caseSensitive”: false
}
}
]
},
{
“type”: “APPLICATION”,
“enabled”: true,
“valueFormat”: null,
“propagationTypes”: ,
“conditions”: [
{
“key”: {
“attribute”: “WEB_APPLICATION_NAME”,
“type”: “STATIC”
},
“comparisonInfo”: {
“type”: “STRING”,
“operator”: “CONTAINS”,
“value”: “test.test.test.test”,
“negate”: false,
“caseSensitive”: false
}
}
]
}
],
“entitySelectorBasedRules”:
}

I’ve already tried:

I tried in my 1st GET call in the test section to create a collection variable. The below code did work and created the variable autotag_id

var resp = pm.response.json();
autotag_id = ;
for (var i=0; i<resp.values.length; i++)
{
autotag_id.push(${resp.values[i].id});
pm.collectionVariables.set(‘autotag_id’, (autotag_id));
}

Result:

How can I use this variable and loop all the values in the 2nd GET call, strip the info and use it into the 3rd call.?

Add this to test script of first request

const idList = pm.response.json().values.map((item)=>item.id)
pm.environment.set("idList",idList)

In pre request of second request add :

const idList = pm.environment.get("idList")
id = idList.pop()
pm.environment.set("idList",idList)
idList.length ? postman.SetNextRequest(pm.info.requestName) : null

This will iterate second request for all id from first reqeust

1 Like

Hi @praveendvd

In the second request I receive:

There was an error in evaluating the Pre-request Script:TypeError: Cannot read property ‘pop’ of undefined

What’s the error
Could you paste the err

Hi @praveendvd

I forgot to paste it :smiley:

There was an error in evaluating the Pre-request Script:TypeError: Cannot read property ‘pop’ of undefined

Pitn idlist and see what value you are getting in first and second request …

Note you should use collection runner for the code to work

Hi

Its been a while since i could react to this. I am still facing an issue. So when i run this API the following happens:

1st request “GET Automatically applied tags”:

2nd request “GET Properties of the specified auto-tag”:

When I run the collection i receive the following:

image

The environment variables are: