How to extract a value from response array and store in an environment variable

Hello All,

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”
}
]
}
]
}
]

@oneappuser

The basics on storing responses are covered in the Postman learning available through the learning center on the Postman homepage.

image

Skip down to Postman Training on the next page.

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.

image

For more info on variables.

Thanks for your suggestion @mdjones , 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.

@oneappuser

var array = [
    {
        "id": "49fcd-180f-432323-99dc-b370a3236ee22f2",
        "category": {
            "id": "tariffMobilePostpaid"
        },
        "relatedParty": {
        "id": "MF_51922"
        },
        "relatedProduct": {
        "id": "MF_78732"
        },
        "items": [
            {
                "priority": 323,
                "offerings": [
                {
                    "id": "MF_199996"
                }
                ]
            }
        ]
    }
]

pm.environment.set("priority", array[0].items[0].priority);
console.info(pm.environment.get("priority"));

pm.environment.set("offerings", array[0].items[0].offerings[0].id);
console.info(pm.environment.get("offerings"));

The results from the console;

image

Thanks @mdjones 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.

@oneappuser

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.

Sure @mdjones … Thanks for your suggestions and valuable time you put in this. Will check and go through those learning sites.

Hi @mdjones , 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.

var array = JSON.parse(JSON.stringify(respBody));

Thanks for you suggestion.

@oneappuser

This is where my JavaScript skills are lacking.

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.

var objectTest = 
    {
        "id": "49fcd-180f-432323-99dc-b370a3236ee22f2",
        "category": {
            "id": "tariffMobilePostpaid"
        },
        "relatedParty": {
        "id": "MF_51922"
        },
        "relatedProduct": {
        "id": "MF_78732"
        },
        "items": [
            {
                "priority": 323,
                "offerings": [
                {
                    "id": "MF_199996"
                }
                ]
            }
        ]
    }

console.log(objectTest);

pm.test('ObjectTest is an object', () => {
    pm.expect(objectTest).to.be.an('object');
});

console.log(objectTest.items[0].priority);
console.log(objectTest.items[0].offerings[0].id);

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.

1 Like