sudhir ranjan Swain
sudhir ranjan Swain

Reputation: 63

How to extract data from a Postman response conditionally and use it in next request

I want to extract data from a Postman response conditionally and use it in next request.

This is my JSON response :

{ 
    "tags": [ 
    "common" 
    ], 
    "launchUrl": [ 
    { 
    "bacId": "default", 
    "launchUrl": "NA" 
    } 
    ], 
    "_id": "5b167cf878ab370336b27588", 
    "name": "IBM Security as a Service_Test RD", 

    }, 
    { 
    "tags": [ 
    "common" 
    ], 
    "launchUrl": [ 
    { 
    "bacId": "default", 
    "launchUrl": "NA" 
    } 
    ], 
    "_id": "5b41b8df36dec2733de9999e", 
    "name": "Automated API Service", 

    } 
    ] 
}

What I want is based on "name" I want to get the id(Which is random) and use it my my next request. Ex: If name is "Automated API Service" get the id , store it in a variable and use it in next request.

Any suggestion would really help

Upvotes: 1

Views: 1016

Answers (1)

Danny Dainton
Danny Dainton

Reputation: 25921

The JSON you have added is not valid but If it's what I think it is, this should set the value.

_.each(pm.response.json(), (arrItem) => {
        if (arrItem.name === "Automated API Service") {
           pm.environment.set("ID", arrItem._id) 
        }
    })

If you add this to the Tests tab, you will be able to use {{ID}} in the next request.

Upvotes: 2

Related Questions