Kishore
Kishore

Reputation: 139

How to access a particular set of keys in array of Json values which i get as response?

For a particular API , I get a response which is similar to the following .

[
    { "name":"Ford", "model":"Mustang"  },
    { "name":"BMW", "model": "320" },
    { "name":"Fiat", "model": "500" }
]

I want to store the values for the key 'name' in a separate variable .

Upvotes: 1

Views: 593

Answers (1)

Peter Thomas
Peter Thomas

Reputation: 58153

Please read the documentation on using JsonPath carefully: https://github.com/intuit/karate#get

Here is an example which works with your data:

* def response = 
"""
[
    { "name":"Ford", "model":"Mustang" },
    { "name":"BMW", "model": "320" },
    { "name":"Fiat", "model": "500" }
]
"""
* def names = $[*].name
* match names == ['Ford', 'BMW', 'Fiat']

Upvotes: 1

Related Questions