Extract value from response

I am trying to extract the value from json response to a variable and use it different api call.
I am always getting response as {equipmentId: 35001565}
I need only 35001565 in my variable.

I am new to postman and started using a week back, tried looking for solution and most of the results are about arrays, so posting a new thread here

Welcome to the community @ereddy068!

Are you familiar with JSON at all? That’s going to make your Postman life significantly easier.

If you add this script into the Tests tab, it will add the equipmentId as a collection variable:

const jsonData = pm.response.json();
pm.collectionVariables.set('equipmentId', jsonData.equipmentId);

Then in any request you need to use that on, you can just use {{equipmentId}} to reference it.

1 Like

Thank you allen.

I was in a belief that postman will give autosuggestion of values if we give . at the end of jsondata.
Now I understood the process.

1 Like

Hi @allenheltondev , if there are nested values, for eg,
sort": [
{
“direction”: “ASC”,
“property”: “name”,
“ignoreCase”: false,
“nullHandling”: “NATIVE”,
“ascending”: true
}

How can I extract the value of ‘direction’ and use it for further assertions?
Tried using : pm.expect(jsonData.direction).to.eql(“ASC”);
got error : AssertionError: expected undefined to deeply equal ‘ASC’

Hey @ashuhhh38,

Your response is an array, so you’ll have to handle it by index. Do you know if you will always only have one result? If so, you could update your assertion to:

pm.expect(jsonData.sort[0].direction).to.equal('ASC'))

If there will sometimes be multiple values, you’ll have to figure out what you want to do there.