Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get particular value from json list use javascript in postman testing

I am using postman to do API testing. My response message is

[
    {
        "firstName": "Jia",
        "lastName": "Tes",
        "memberId": 745794,
        "branchId": 12442,
        "branchName": "NZ - Clubware Mobile Test Branch 1"
    },
    {
        "firstName": "Jia",
        "lastName": "Test2",
        "memberId": 745746,
        "branchId": 12442,
        "branchName": "NZ - Clubware Mobile Test Branch 1"
    },
    {
        "firstName": "Jia",
        "lastName": "Test3",
        "memberId": 745748,
        "branchId": 12443,
        "branchName": "Clubware Mobile Test Branch 2 (Pub)"
    },
    {
        "firstName": "Jia",
        "lastName": "Test3",
        "memberId": 745745,
        "branchId": 12442,
        "branchName": "NZ - Clubware Mobile Test Branch 1"
    }
]

I would like to get "memberId" where "firstName": "Jia","lastName": "Test3" and "branchName": "NZ - Clubware Mobile Test Branch 1"

my current code is like

var response = JSON.parse(responseBody);
console.log("BODY:" + response[3].memberId);

but i don't like use index to locate element in list, how can I do that, thank you guys!!

like image 549
Yu Jia Avatar asked Sep 07 '25 08:09

Yu Jia


1 Answers

If you just wanted to check for a specific value, you could use the _.find() Lodash function to get the memberId value:

var user = _.find(pm.response.json(), { 
    firstName: "Jia", 
    lastName: "Test3", 
    branchName: "NZ - Clubware Mobile Test Branch 1" 
})
console.log(user.memberId)

Postman Response

like image 70
Danny Dainton Avatar answered Sep 09 '25 04:09

Danny Dainton