Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Filtering values from a json get response

I am relatively new in Postman and I am having troubles filtering my response. I tried various methods but either I am going the wrong way or I am doing it wrong.

[
     {
         "id": "62f9-4fe5-82c4-4ecf",
         "description": "BLUE",
         "original_name": "AZURE_BLUE"
     },
     {
         "id": "98cd-95ed-45e9-a855",
         "description": "GREEN",
         "original_name": "LEAF_GREEN"
     }
   ]

What I am trying to do is to get the id value of Azure_Blue and pass it in a global variable to later use it on a different API call but I am stuck.

I tried

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

const obj = JSON.parse(responseBody)
if(obj.original_name ==='AZURE_BLUE'){
    var subj=obj.id;
    postman.setGlobalVariable("id", subj);
}

and

pm.test("Get id", function () {
if(pm.response.code === 200){
    var obj = JSON.parse(responseBody);
    _.each(pm.response.json().data,(item) =>{
        if(item.original_name === 'AZURE_BLUE'){
            var subj=obj.id;
            postman.setGlobalVariable("id", subj);
        }
    })
}
});

Any help is greatly appreciated

>Solution :

This would work

const res = JSON.parse(responseBody)
const obj = res.find(({original_name}) => original_name === 'AZURE_BLUE');
pm.globals.set('id', obj.id);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading