4

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!!

2
  • 2
    try response.find(e => e.firstName === 'Jia' && e.lastName === 'Test3' && ... etc).memberId - for more about Array find method read the documentation Commented Jun 14, 2018 at 0:39
  • @JaromandaX, thank you very much, your suggestion is great! Commented Jun 14, 2018 at 1:04

2 Answers 2

8

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

Sign up to request clarification or add additional context in comments.

Comments

2
var response = [    {
        "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"
    }
]; var i;

for(i =0; i <response.length ; i++)
{ 
  if(response[i].firstName == 'Jia' && response[i].lastName == 'Test3' 
     && response[i].branchName == 'NZ - Clubware Mobile Test Branch 1')
  {
    console.log(response[i].memberId);
  }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.