How can I set Id value to environment variable from the response when there is array?

This is covered in the training within the Learning Center on the page Postman page after you login.

Go to Postman Training and I would start with “Galaxy APIs 101” then the “Galaxy Testing and Automation” courses.

Introduction | Postman Learning Center

Also check out the following link.

Using variables | Postman Learning Center

The code you need in the test tab of your request is as follows;

// var response = pm.response.json();
var response = 
{
    "Message": "Successfully ",
    "Status": 0,

    "ResponseCode": 0,

    "Data": [
        {
            "ID": 10675,
            "cx_lName": "Name2",
            "cx_Folder": "Folder"
        }
    ]
}

console.log(typeof(response)) // this should be a javascript object, same as pm.response.json()
console.log(response); // whats in that object

console.log(response.Data[0].ID); // first element in the data array - arrays start at zero

pm.environment.set("ID", response.Data[0].ID); // save it to an environment variable

console.log(pm.environment.get("ID")); // console log that environment variable
1 Like