Reputation: 63
I'm trying to save certain values from one big array to the postman environment to compare these to the values of another postman request.
This is the response code, it's one big array. I want to save all the values of metersToNextBollard as an environment variable with a loop.
[
{
"id": "",
"bicsCode": "",
"number": "",
"coordinates": {
"latitude": 0,
"longitude": 0
},
"metersToNextBollard": 20.15,
"mooringSide": "starboard"
},
{
"id": "",
"bicsCode": "",
"number": "",
"coordinates": {
"latitude": 0,
"longitude": 0
},
"metersToNextBollard": 24.85,
"mooringSide": "starboard"
},
{
"id": "",
"bicsCode": "",
"number": "",
"coordinates": {
"latitude": 0,
"longitude": 0
},
"metersToNextBollard": 18.2,
"mooringSide": "starboard"
},
At this moment I'm using this code without a loop, but I think it can be much cleaner.
pm.test("Response is ok", function () {
pm.response.to.have.status(200);
});
const resBody = pm.response.json();
postman.setEnvironmentVariable("LT1-B1 MCA Afstand", resBody[0].metersToNextBollard);
postman.setEnvironmentVariable("LT-B19 MCA Afstand", resBody[18].metersToNextBollard);
postman.setEnvironmentVariable("DBF-D15 MCA Afstand", resBody[19].metersToNextBollard);
postman.setEnvironmentVariable("DBF-D98 MCA Afstand", resBody[102].metersToNextBollard);
postman.setEnvironmentVariable("DDE-1 MCA Afstand", resBody[103].metersToNextBollard);
postman.setEnvironmentVariable("DDE-128 MCA Afstand", resBody[230].metersToNextBollard);
postman.setEnvironmentVariable("DDN-178 MCA Afstand", resBody[231].metersToNextBollard);
postman.setEnvironmentVariable("DDN-224 MCA Afstand", resBody[277].metersToNextBollard);
Edit:
The first loop works, but now I'm trying to do the same in another request, the response body looks like this:
"bollards": [
{
"id": ,
"number": 1,
"description": "DBF-D15",
"position": {
"latitude": 0.0,
"longitude": 0.0
},
"metersToNextBollard": 11.48,
"phantomBollard": false,
"mooringSide": "STARBOARD"
},
{
"id": ,
"number": 2,
"description": "DBF-D16",
"position": {
"latitude": 0.0,
"longitude": 0.0
},
"metersToNextBollard": 3.28,
"phantomBollard": false,
"mooringSide": "STARBOARD"
},
{
"id": ,
"number": 3,
"description": "DBF-D17",
"position": {
"latitude": 0.0,
"longitude": 0.0
},
"metersToNextBollard": 11.48,
"phantomBollard": false,
"mooringSide": "STARBOARD"
},
{
"id": ,
"number": 4,
"description": "DBF-D18",
"position": {
"latitude": 0.0,
"longitude": 0.0
},
"metersToNextBollard": 13.12,
"phantomBollard": false,
"mooringSide": "STARBOARD"
},
{
"id": ,
"number": 5,
"description": "DBF-D19",
"position": {
"latitude": 0.0,
"longitude": 0.0
},
"metersToNextBollard": 11.48,
"phantomBollard": false,
"mooringSide": "STARBOARD"
},
{
"id": ,
"number": 6,
"description": "DBF-D20",
"position": {
"latitude": 0.0,
"longitude": 0.0
},
"metersToNextBollard": 3.28,
"phantomBollard": false,
"mooringSide": "STARBOARD"
},
{
"id": ,
"number": 7,
"description": "DBF-D21",
"position": {
"latitude": 0.0,
"longitude": 0.0
},
"metersToNextBollard": 11.48,
"phantomBollard": false,
"mooringSide": "STARBOARD"
},
Upvotes: 3
Views: 6072
Reputation: 928
I assume these envirement variables should be unique and looking at the docs I can see that number
returns the "DDE-59" value. Looking at your API JSON data, it seems each value is unique and after the number
is just pasted "MCA Afstand".
If this is all true, you could create a for loop for your data as following:
const resBody = pm.response.json();
for (let i = 0; i < resBody.length; i++) {
postman.setEnvironmentVariable(resBody[i].number + "MCA Afstand", resBody[i].metersToNextBollard);
}
Except you REALLY need the "MCA Afstand" I would say remove it from the variable name. As its just overhead and can be added whenever you are going to use the variable again.
Upvotes: 3