简体   繁体   中英

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

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!

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'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM