rajs2020
5 years agoFrequent Contributor
Groovy best practices - How to set the root node when extracting data from a response?
See this code:
def jobDate = context.expand( '${GetJobInfo#Response#$[\'jobData\'][0][\'jobDate\']}' )
I see this kind of code too many times in a codebase. For example, it will be repeated for each variable inside jobData json object. This is an eyesore for the reader and makes it harder to understand the code.
Is there a way to set the root node in context.expand(...)? So, I could set the root node as jobData[0] and refer to all the elements inside it without using the full path.
I guess I could also create a variable rootPath and keep changing it as needed. Would that work?
rajs2020 : If it is an array, then instead of using context of expand you can use jsonSlurper to get the value from the array by iterating over each element. Refer below link for documentation