Below is the response I am getting from my API. I wan the priority and offering id value to be stored in an env variable from this response. Please help on this.
[
{
“id”: “49fcd-180f-432323-99dc-b370a3236ee22f2”,
“category”: {
“id”: “tariffMobilePostpaid”
},
“relatedParty”: {
“id”: “MF_51922”
},
“relatedProduct”: {
“id”: “MF_78732”
},
“items”: [
{
“priority”: 323,
“offerings”: [
{
“id”: “MF_199996”
}
]
}
]
}
]
From here, I recommend the Galaxy APIs 101, and Galaxy Testing and Automation as this covered all of the basics for me. This will include storing environment variables.
Thanks for your suggestion @michaelderekjones , I have implemented in other scenarios and know how to use env variable and store the value. But facing issue with this array. Its storing value as null.
Thanks @michaelderekjones for the solution. But I have one doubt. If we will store this exact response in a variable then for other scenarios how will it work. Because everytime it will return the same value as I am having different data sets to verify.
Your original post asked “I want the priority and offering id value to be stored in an env variable from this response”.
The solution above is doing exactly this.
Variables are set in this way to ensure they contain the correct data. That they are consistent. They only get overwritten if you specifically code this.
You will have to explain a bit more about these other scenarios before we can give any further advice.
Do you mean the same http request (get\post) with different information. (In which case look up data driven testing).
You haven’t really explained how you want to use the variables that were set. Do you want to use them in a subsequent request which is basic functionality and covered in the Learning Center.
On a side note, manipulating arrays to find information is more JavaScript than Postman. Postman leverages JavaScript in the test and pre-test tabs. It’s worth at least going through the WC3 Schools basics on HTML, CSS and JavaScript if you are going to use Postman a lot. I know I need to go back through the JavaScript course myself to refresh myself. I’m not a JavaScript developer, but know enough to read others code and work out the rest. The refresher will help.
Hi @michaelderekjones , thanks for your code snippet. It helped me to solve this with a lil bit of change from myside. So instead of storing the array in a variable I just parsed it.
JSON.stringify turns a JavaScript object or value to a JSON string.
The array I listed in my example was a JavaScript ‘array’ as I created it that way so just I could test the responses in the Test tab without trying to mock the response. From the initial look at your example, it looked like an array. [ Remove the square brackets ] and it will turn into an JavaScript object. (Not a string).
I assumed that you already parsed the response into a variable using const response = pm.response.json(); or similar.
JSON.parse(responseBody) and pm.response.json() do the same thing.
They both return the response body of your API request as a JavaScript object in JSON format. (so not technically an array at this point).
Although you can navigate the structure in a similar way.
The following returns the same results as previously.
I did look at XPATH and JSONPath options within Javascript as that is what I’m used to with SoapUI, but until Postman provides native support for an existing library, I’ve decided to stick with the JavaScript arrays which for the most part I can now navigate and get the elements I want, either directly as in the example I provided previously or by using find\map where I search for elements and return the results as an array.
In your example, I’m not sure the stringify is needed. You should just be able to parse the response and work with it as a JavaScript object.