Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to retrieve all values for a particular key in karate

Tags:

karate

How to retrieve partNumbers from below response. In below response

  "10000061","10000062","10000063"

are dynamic in nature. I have to match these partNumbers with data table partnumbers.( In a response there could be more than 10 part numbers(based on input) and i have to validate them.)

{ "added": true, "lineItems": { "1111111": { "itemCore": { "partNumber": 
"10000061" } }, "222222": { "itemCore": { "partNumber": "10000061" } }, 
 "3333333": { "itemCore": { "partNumber": "10000063" } } } } 

Tried below

def partNum= get[0] response..itemCore.partNumber[*] but getting empty array.
def partNum= get[0] response..itemCore.partNumber but getting empty value.

My below second approach also giving me empty value.

 * def keys = function(obj){ return response.lineItems.keySet() }
 * json dynamicValue= keys(response)
 * print 'dynamic value '+dynamicValue
 * def first = dynamicValue[0]
 * print response.lineItems.dynamicValue[0].itemCore.partNumber
 * print response.lineItems.first.itemCore.partNumber
like image 969
sam Avatar asked Jan 01 '23 16:01

sam


2 Answers

For retrieving data for a particular key, you can use deep scan operator in jsonPath,

* def partNumbers = karate.jsonPath(response,"$..partNumber")
like image 163
Babu Sekaran Avatar answered May 11 '23 09:05

Babu Sekaran


Here's another solution, using karate.forEach() which can also operate on a map, not just a list:

  * def keys = []
  * eval karate.forEach(response.lineItems, function(k){ keys.add(k) })
  * print keys
like image 36
Peter Thomas Avatar answered May 11 '23 09:05

Peter Thomas