Req2Ans
Req2Ans

Reputation: 11

How can I set an environment variable in postman from the response data received?

My response body data:

  "slots": {
      "deliverySlots.today.text": {
        "list": [
          {
            "pos": "004",
            "deliverySlotCode": "8800652143353",
            "deliveryDate": "Mar 20",
            "beginTime": "10 am",
            "endTime": "12 pm",
            "dayOfWeek": "Tuesday",
            "selected": false,
            "expired": true,
            "isAvailable": true
          }
        ]
      }
    }

I would like to save the value of the isAvailable property into an environment variable. How can I set this value?

Upvotes: 1

Views: 329

Answers (1)

Danny Dainton
Danny Dainton

Reputation: 25921

Given the data that you posted, you could add something like this to the Tests tab to create the variable.

var my_value = pm.response.json().slots['deliverySlots.today.text'].list[0].isAvailable
pm.environment.set('my_var', my_value)

This will only set the value that is in position 0 in your list array.

This part of the route deliverySlots.today.text needs to be wrapped in brackets [] as you cannot use the same dot notions to parse the JSON. It's quite a terrible naming convention so if you have any say over it, i'd recommend changing it.

Upvotes: 1

Related Questions