How to set environment variables from an array?

Hi, very little code experience, but use the Postman tool a lot to test our app that uses REST API Web Services.
I’m trying to set environment variables from JSON Response that are in an array.
Works fine if the Response attributes are not in an array. (2nd example)

JSON response :

“Vendors”: [
{
“VendorId”: 4515,
“AssignmentType”: “NONE”,
“LeadCounselFlag”: false,
“VendorName”: “Boston and Maine Law”,
“FirmId”: “TESTCMS-01-TEST-XYZ”,
“AssignDate”: “2019-10-31”,
“VendorType”: “LAWFIRM”,
“FeeAgreement”: “NA”,
“Designation”: “PANEL”
},
{
“VendorId”: 4671,
“AssignmentType”: “NONE”,
“LeadCounselFlag”: false,
“VendorName”: “A B & V LLP”,
“FirmId”: “113640790”,
“AssignDate”: “2019-12-31”,
“VendorType”: “LAWFIRM”,
“FeeAgreement”: “NA”,
“Designation”: “PANEL”
}
]
}

If variable is NOT in an array (only one possible value for each attribute) the following works:
JSON Response:
{
“CaseId”: 4230,
“CaseName”: “123456789JS”,
“CaseNumber”: “123456789JS”,
“CaseClass”: “Auto Injury”,
“ActiveDate”: “2019-03-09”,
“InActiveDate”: “2020-03-09”,
“CaseManagerEmpID”: “NJDELUCA”,

var data = JSON.parse(responseBody);
postman.setEnvironmentVariable(“CaseId”, data[0].CaseId);
postman.setEnvironmentVariable(“CaseName”, data[0].CaseName);
postman.setEnvironmentVariable(“CaseNumber”, data[0].CaseNumber);
postman.setEnvironmentVariable(“CaseClass”, data[0].CaseClass);
postman.setEnvironmentVariable(“ActiveDate”, data[0].ActiveDate);
postman.setEnvironmentVariable(“InActiveDate”, data[0].InActiveDate);
postman.setEnvironmentVariable(“CaseManagerEmpID”, data[0].CaseManagerEmpID);

postman.setEnvironmentVariable(“CaseId”, data[0].CaseId);

Thank you in advance!

Hey @njdeluca :wave:

Please let me know if the following works for you.

const resp = pm.response.json()
const vendorsArray = resp.Vendors
pm.environment.set('VendorId', vendorsArray[0].VendorId) //access the first object in the array

I hope this helps :smiley:

Thanks. Script fails with error: TypeError: Cannot read property ‘0’ of undefined

Hey @njdeluca,

The error you are seeing suggests vendorsArray is undefined.
Can you send me the output of console.log(pm.response.json())?

Hi, appreciate your response, as it set me in the right direction.
I found a tool that helps - https://jsonpathfinder.com/

Just paste the JSON Response and it shows you the path

[
{
“CaseId”: 1826,
“CaseName”: “Insured Name”,
“CaseNumber”: “112345”,
“CaseClass”: “Auto Injury”,
“ActiveDate”: “2012-10-02”,
“CaseManagerEmpID”: “123456”,
“CaseTypes”: [
{
“MttId”: 2208,
“CaseTypeDesc”: “AL”
},
{
“MttId”: 984,
“CaseTypeDesc”: “FIRE AND CASUALTY”,

        }
    ]

… a snippet ,

bodydata = JSON.parse(responseBody)
value = bodydata[0].CaseId
pm.environment.set(“CaseId”, value)
console.log(value)
value = bodydata[0].CaseId
pm.environment.set(“CaseId”, value)
console.log(value)
value = bodydata[0].CaseTypes[0].CaseTypeDesc
pm.environment.set(“CaseTypeDesc”, value);
console.log(value);
value = bodydata[0].CaseTypes[1].CaseTypeDesc
pm.environment.set(“CaseTypeDesc”, value);
console.log(value);

I’m a novice hacker and I’m sure there’s a better way to loop the variables?

Thank you!