Ask a Question

Getting error with dynamic extractions in Groovy script


Getting error with dynamic extractions in Groovy script


In Groovy Script trying to extract value from API Response . Instead of getting first value in Json trying to extract dynamically by referring to its value from datasource.

Ex- def sourceId = context.expand( '${GetSources#Response#$[0][\'SourceId\']}' )

Here instead of extracting 0th element, I need to get sourceId based on sourceCode. 


Sources is datasource name

So I tried below 3 different ways but error is thrown. Attached error.

1. def sourceId = context.expand( '${GetSources#Response#$[[?@.SourceCode=='${Sources#SourceCode}')].SourceId][\'SourceId\']}' )


2. def sourceId = context.expand( '${GetSources#Response#$[[?(@.SourceCode=='${Sources#SourceCode}')].SourceId]}')


3.def sourceId = context.expand( '${GetSources#Response#$[?(@.SourceCode=='${Sources#SourceCode}')].SourceId]}')


Please suggest the correct syntax .

Here is format of GET API Response

"SourceId": "bc3cef1e-a9f1-46df-a4f0-c1131357ea57",
"SourceCode": "ABC",
"CodeTier": "123",
"SourceDescription": "ABC Desc"
"SourceId": "b4035134-ca33-4b33-b3c7-06ea880f1f28",
"SourceCode": "DEF",
"CodeTier": "456",
"SourceDescription": "DEF Descr"

"SourceId": "9666bd19-1916-4052-9044-06b0e38e9175",
"SourceCode": "GHI",
"SourceDescription": "GHI Descr",






Thanks for trying it out.

Have updated the script in the above post to handle NPE.
Please take the updated one and check.


Hello nmrao,

I am not able to find the updated post with NPE. Could you please check and post it again.

Its trivial change. Edit was done in the code of the post. That's what I said. Just compare the code you tried.

You won't be able to notice the change unless you look at character by character.


Thanks nmrao. It worked.

Could you suggest any library or link ,where I can refer more on these Json response  , as there would be more of these kind of scenario's to handle. Thank you in advance.

Thank you for your patience and trying it out.
You can google for apache-groovy documentation.
Also you can find different sample on my github\ (shared in first reply) repo.

Appreciate if you mark "Accept the Solution" for the actual reply.

Showing results for 
Search instead for 
Did you mean: