Home > database >  How to get object from array when you don't know the index number
How to get object from array when you don't know the index number

Time:01-19

so here is my Actual response of get call

{"message": { "Name": "Rodger", "Salary": "5000" "id": 54g545f5dfd5e5d }, { "Name": "Novak", "Salary": "4000" "id": 62g345f7dfd3e5g } }

later using post call I added another person who has same name and salary. and its get call response look like

{"message": { "Name": "Rodger", "Salary": "5000" "id": 54g545f5dfd5e5d }, { "Name": "Rodger", "Salary": "5000" "id": 27g145f5dfd4e5k }, { "Name": "Novak", "Salary": "4000" "id": 62g345f7dfd3e5g } }

So now I want to get that object who's id is 27g145f5dfd4e5k. Note: I don't know the index so using id can I get that person.

Thank you in advance!

CodePudding user response:

I recommend you spend some time understanding JSON and how to use JS in Karate to access data.

* def response = { message: [ { name: 'foo1', id: 'bar1' }, { name: 'foo2', id: 'bar2' } ] }
* def id1 = response.message[0].id
* match id1 == 'bar1'
  
* def id2 = response.message[1].id
* match id2 == 'bar2'
  • Related