Variable Not being created when response body has []

Hi,

I’m trying to set a variable from my response, but while the variable is being created there is no value assigned… below is my response

image

Here’s my variable set
var jsonData = JSON.parse(responseBody);postman.setGlobalVariable(“TZDelDoor”, jsonData.ID);

and here is my variable that has been created

image

any ideas?

My variables get created without any issues when the api response doesn’t have []

John

@JohnF75 This is because your response is in an array.

Try something like this:
var jsonData = JSON.parse(responseBody);postman.setGlobalVariable(“TZDelDoor”, jsonData[0].ID);

2 Likes

cheers tmccann… :slight_smile:

Further to this… I am trying to create a variable from an array this is within curly brackets… and successfully failing … lol … any assistance??? below is my response
image

I’m trying to create variables based on the highlighted “Site” fields

const body = pm.response.json();
pm.globals.set("site1", body.MusteredCardHolders[0].Site);
pm.globals.set("site2", body.MusteredCardHolders[1].Site);
1 Like

Cheers for that pfarrell :wink:

No worries mate.

Whenever you’re dealing with an array that contains objects, you want to specify in the square brackets which item in the array you want to target - the first item in the array is 0, the second is 1 etc.