jsonpathRetrieve array element in an array from JSON response
Hi All,
I'm trying to retrieve an array element within an array (sample response attached) from a test step in a test case. Within this response, I want to retrieve an array of Telecoms values where the SystemCode ==phone and then get the first element from this array to use in another step.
I tried doing this with a property transfer step, but the problem is that when I retrieve the array using this JSONpath as the source, it returns an empty array even though I it should return an array of 25 elements.
$..Telecoms[?(@.SystemCode=="phone")].ValueString
So, I'm trying to do this with Groovy script. I'm not familiar with Groovy, so I'm cobbling this together based on what I've been reading. I know the syntax isn't correct, but I'm hoping I can get some help to get what I need with this bit of pseudocode:
import groovy.json.JsonSlurper //retrieve response from the 'All Matches' test step def response = context.expand( '${TestStepName#AllMatches}' ).toString() def slurper = new JsonSlurper() def json = slurper.parseText(response) //get an array of values where the Telecoms.SystemCode = 'phone' def phoneArray = log.info(json.Itemlist.Telecoms[?(@.SystemCode=="phone")].ValueString) //get the first element from phoneArray def phoneElement = jsonParse[0]
The result of JSONPath expression $..Telecoms[?(@.SystemCode=='phone')].ValueString has more than 25 elements because there are different use codes. Do you want to filter only mobile or home phone numbers?
$..Telecoms[?(@.SystemCode=='phone') && @.UseCode=='mobile')].ValueString
If the use code for the first element doesn't matter then
$..Telecoms[?(@.SystemCode=='phone')][0].ValueString