Forum Discussion
CharlesHarold
Occasional Contributor
I should point out that (most of) the parentheses and spaces are not necessary; the following will work the same:
log.info " Count of people returned: " + parsedJson.size() log.info " Was Will's info returned (exists)? " + ( parsedJson.find{it.firstName=='Will'} != null ) log.info " Was Alice's info NOT returned (not exists)? " + ( parsedJson.find{it.firstName=='Alice'} == null ) log.info " First person's first name: " + parsedJson[0].firstName log.info " Index of person with ID 746381: " + parsedJson.findIndexOf{it.uniqueId==746381} log.info " Info for person with last name Tell: " + parsedJson.find{it.lastName=='Tell'} log.info " Jack's ID: " + parsedJson.find{it.firstName=='Jack'}.uniqueId log.info " Jack Jones's job title: " + parsedJson.find{it.firstName=='Jack'&&it.lastName=='Jones'}.jobInfo.title log.info " All peon job type people's first names: " + parsedJson.findAll{it.jobInfo.type=='Peon'}.firstName log.info " Is Will's thisIsAlwaysNull null? " + ( parsedJson.find{it.firstName=='Will'}.thisIsAlwaysNull == null ) log.info " Will Tell's had this many reviews: " + parsedJson.find{it.firstName=='Will'&&it.lastName=='Tell'}.reviews.size() log.info " Bob's 2017-06-01 review rating: " + parsedJson.find{it.firstName=='Bob'}.reviews.find{it.date=='2017-06-01'}.rating
And if you want to dynamically build the code to get a specific value as a string, and then evaluate it as code, the following works:
def codeSnippet = "parsedJson.find{it.firstName=='Jack'&&it.lastName=='Jones'}.jobInfo.title" log.info " Evaluate string as code: " + Eval.me( "parsedJson", parsedJson, codeSnippet )
sprice090161
7 years agoContributor
Thank you for the example. I have a json response containing multiple objects and I need to determine the index of the object that I want. "findIndexOf" will be quite helpful.
Thank You
Steve Price
Related Content
- 6 years ago
- 9 months ago
Recent Discussions
- 5 days ago
- 9 days ago