1

I am trying to set a postman environment variable based on a specific value in a response.

I'm unsure on code to use use to grab value.

I know I need to set the response as a variable which I have done as follows:

var response = JSON.parse(responseBody);

And I know I can use the following to set my environment variable:

postman.setEnvironmentVariable("category_ref",myVariableName);

Below is a snippet from my response:

{
    "id": 45,
    "name": "Accommodation",
    "description": ""
},
{
    "id": 46,
    "name": "Accommodation (Engineering)",
    "description": ""
},

I want to grab the "id" value based on "name" value which I will already know.

So an example being I want my code to give me the ID where "name" = "Accommodation"

Edit:

Changed made to original question following answers below.

My Tests code now looks like this:

//Ensure the API Test Category is present
var response = JSON.parse(responseBody);
tests["my test"] = responseBody.has("Accommodation");

//pass in id into variable for delete step
var requiredId = pm.response.json().find(function(element){
    if (element.name == "Accommodation"){
        return element.id;
    }
});

stringId = JSON.stringify(requiredId);

pm.environment.set("category_ref",stringId);

console.log("my string "+stringId);

And my output to console looks like the following which is also the value that is being sent to the category_ref environment variable:

my string {"id":45,"name":"Accommodation","description":""}

The remaining problem is I don't want to return all the elements as it is doing above, I am wanting to return just "45" which is the id value where name = Accommodation.

Henke
  • 4,445
  • 3
  • 31
  • 44
Matt
  • 501
  • 1
  • 12
  • 26
  • Does this answer your question? [Postman get value from JSON where equals a value in array using javascript](https://stackoverflow.com/questions/54901902/postman-get-value-from-json-where-equals-a-value-in-array-using-javascript) – Henke Feb 13 '21 at 13:17

1 Answers1

1

Tests in Postman are noting but the JavaScript, so you can use Array.find() as follows,

Response Body:

[
    {
        "id": 45,
        "name": "Accommodation",
        "description": null
    },
    {
        "id": 46,
        "name": "Accommodation (Engineering)",
        "description": null
    }
]

Test window:

var matchedItem = pm.response.json().find(function(element) {
    if (element.name == "Accommodation") {
        return element;
    }
});

console.log(matchedItem.id);
Divyang Desai
  • 7,483
  • 13
  • 50
  • 76
  • Thanks. This is great. I try to show my value with `console.log("my string "+requiredId);` but the output is `my string [object Object]` so i wonder if i need to convert to a string or something? – Matt Apr 05 '19 at 06:35
  • Getting closer. stringify worked. But now my output is `my string {"id":45,"name":"Accommodation","description":""}` where as I am hoping to see a value of `45` only. – Matt Apr 05 '19 at 06:48
  • Yes, definitely using that. Pasted from code here - `if (element.name == "Accommodation"){ return element.id;` – Matt Apr 05 '19 at 07:02
  • Sorry. I only see a change to the var name in the Test window and no reference to the array find. – Matt Apr 05 '19 at 07:56
  • 1
    Thanks so much for all this help. Working perfectly and taught me for next time. – Matt Apr 05 '19 at 08:13
  • Glad I could help! sure, throw a question link anytime :) – Divyang Desai Apr 05 '19 at 09:01
  • Struggling to tag you in my latest question @Div. Thinking I don't have those permission levels yet. – Matt Apr 09 '19 at 08:24
  • Hey @Matt, no there is no permission levels required, in fact, you cannot tag external person of that thread. I'll look at your latest question once I get some time – Divyang Desai Apr 09 '19 at 08:50