NaiJi
NaiJi

Reputation: 3

Accessing variables inside of response array

I have a GET request which returns an array of bodies:

[
    {
        "id": "79ad1daf-54f0-49cf-8628-7fb38457fdd1",
        "deviceId": "wowSomeRandomName",
        "deviceName": "wowSomeRandomName",
        "iconUrl": "/i/devicepics/smart_tv.svg",
        "lastUse": "2018-12-13T10:05:00.609Z"
    }
]

And I need to set these variables as global variables. I understand how to do that for external bodies outside of an array:

pm.environment.set("deviceUUID", pm.response.json().id);
pm.environment.set("deviceId", pm.response.json().deviceId);
pm.environment.set(. . . .);
and etc.

But what to do when they are inside of [ ]?

Upvotes: 0

Views: 431

Answers (1)

Pavan Skipo
Pavan Skipo

Reputation: 1867

I think you can first convert the response to JSON then operate on that:

var responseData = JSON.parse(response);
pm.environment.set("deviceUUID", responseData[0].id);
pm.environment.set("deviceId", responseData[0].deviceId);
and etc

got this idea from: https://learning.getpostman.com/docs/postman/scripts/test_examples/

please let me know if this doesn't work.

Upvotes: 2

Related Questions