Reputation: 35
While hitting the service in Postman I am getting the multiple blocks in response body, i need to take one element value and need to store it in Environment variable to use it in next request.
Kindly help to take the value of depositInfoId
and need to set in Environment variable.
I have tried with below one but not worked.
pm.environment.set("depositInfoId",pm.response.json().depositInfoList);
pm.environment.set("depositInfoId",pm.response.json().depositInfoList.depositInfoId);
Below is the sample Response body with few blocks:
{
"qualStatus": "PASS",
"blackList": {
"blackListStatus": "PASS",
"blackListStatusDesc": "CustomerName Match Not Found."
},
"fraudCheck": {
"linesActivated": null,
"status": "NA",
"activationDetails": null,
"errorDetails": null,
"creditCheck": {
"intlDialingEligibilityIndicator": true,
"intlRoamingEligibilityIndicator": true,
"creditCheckDetails": [{
"linesApproved": 1,
"approvedTerm": "24",
"accountSpendingLimit": null,
"lineDepositAmount": null,
"securityDepositeAvailableIndicator": null,
"creditLimitProgramIndicator": null,
"noDeviceProgramIndicator": null,
"creditOptionId": null
}],
"status": "APPROVED",
"errorDetails": null,
"accountNumber": "12345678",
"creditAssessmentID": "123456",
"contractTypes": null,
"esimProductCode": null
}
},
"depositInfoList": [{
"depositInfoId": 863,
"transactionID": "123456",
"lineDepositAmount": null,
"linesActivated": "1",
"internationalDailingDeposit": null,
"approvedTerm": "24",
"depositSubscriberCount": null,
"spendingLimit": null,
"creditAssessmentId": "123456",
"securityDepositAvailable": null,
"creditLimitIndicator": null,
"noDeviceProgramIndicator": null,
"creditOptionId": null
}]
}
In Environment variable values stored as below when I use first code.
depositInfoId - [object Object] - [object Object]
When I use second code fields are blank not stored in it.
Upvotes: 0
Views: 4311
Reputation: 552
If you want to store the whole object/array in your Environment/Global/Collection variables, you need to stringify the value first:
const jsonBody = pm.response.json();
pm.environment.set('depositInfoList', JSON.stringify(jsonBody.depositInfoList)); //will store the whole array as string
pm.environment.set('depositInfoItem', JSON.stringify(jsonBody.depositInfoList[0])); //will store first object from the array as string
pm.environment.set('depositInfoItemId', jsonBody.depositInfoList[0].depositInfoId); //will store if of first object from the array
Otherwise, you will store [object Object] value:
const jsonBody = pm.response.json();
pm.environment.set('depositInfoList', jsonBody.depositInfoList); //will store [object Object]
pm.environment.set('depositInfoItem', jsonBody.depositInfoList[0]); //will store [object Object]
pm.environment.set('depositInfoItemId', jsonBody.depositInfoList[0].depositInfoId); //will store if of first object from the array
Secondly, if you want to get value from the Environment/Global/Collection variable and work with it as array/object you need to parse it as JSON:
const depositInfoList = JSON.parse(pm.environment.get('depositInfoList')); //returns array
const depositInfoItem = JSON.parse(pm.environment.get('depositInfoItem')); //returns object
const depositInfoItemId = pm.environment.get('depositInfoItemId'); //returns number
Upvotes: 1
Reputation: 7866
pm.response.json().depositInfoList
This will save whole depositInfoList JSON data to the environment variable, and hence when you try to access it, it showed up Object Object.
pm.response.json().depositInfoList.depositInfoId
This doesn't make sense as depositInfoList
is an array, you have to provide index when you want to fetch values from arrays.
pm.response.json().depositInfoList[0].depositInfoId
This should work, as you're trying to get the property value of the first record of the array depositInfoList
.
Upvotes: 3