Ask a Question

Getting error with dynamic extractions in Groovy script

SOLVED
Hellotest
Contributor

Getting error with dynamic extractions in Groovy script

Hello,

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",
}

 

 

 

 

14 REPLIES 14
nmrao
Champion Level 3


@Hellotest wrote:

Hello,

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]}')

1. Not good at JsonPath way which you are trying to use

2. Some time ago when tried something similar (conditional match) in JsonPath assertion, it wasn't working. The reason could be the that the tool might be using different libraries.

3. In order to get it working as you need, use JsonSlurper.

Please find relevant sample which can be used and come with what is needed in your case.

https://github.com/nmrao/soapUIGroovyScripts/blob/master/groovy/json/QueryRelativeData.groovy



Regards,
Rao.

Hello nmrao 

Actually Assertion are working fine for me with Json Path Match as below-

[?(@.SourceCode=='${Sources#SourceCode}')].SourceId

But when used in Groovy script as I had provided they are not working. So unable to understand what is issue with syntax . 

 

The JsonSlurper example provided does not have  the details from data source.

So it will not apply.

 

 

 

nmrao
Champion Level 3

Get the response into a variable, say jsonString. And pass that to JsonSlurper().parseText(jsonString).
Now you should be able to follow the given example.


Regards,
Rao.

Even though it passes for assertion, I am surprised why it will not work for dynamic extraction.

There should be a better way to get a value from response.

nmrao
Champion Level 3

You can check with customer care by creating a ticket.

Any way you are using groovy script. There can be different ways to get the same.
I have no comments if you want to get in a particular way.

By the way have you tried the script link given in the initialreply.


Regards,
Rao.

I tried first with out datasource as below but getting null pointer error as attached. 

 

import static com.jayway.jsonpath.JsonPath.parse
def jsonString = context.expand( '${GetSources#Response}' )
def sourceId = new groovy.json.JsonSlurper().parseText(jsonString).result.find{it.SourceCode == 'ABC'}
log.info "source id: ${sourceId.SourceId}"

 

Error- java.lang.NullPointerEcxeption: Cannot get property 'source code' on null object  error at line 3 

Not sure what is wrong here.

 

Response JSON

[
{
"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",
}
]

I also tried the syntax issue again.

 

It works when it is hardcoded

def response = context.expand( '${GetSources#Response#$[?(@.SourceCode=="ABC")][\'SourceId\']}' )
log.info(response) // this worked and printed right  value of Id

 

Now adding with data source-

def sourceCode = context.expand( '${Sources#SourceCode}')
log.info(sourceCode)  //this worked and printed right value 

 

Now using datasource , error is throw in below response1 or 2 

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

 or

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

 

Error details- groovy.lang.MissingMethodException: No signature of method: Script53.${GetSources#Response#$[?(@.SourceCode=() is applicable for argument types: (String) values: [PP_WEB] error at line: 10

 

Could any one please suggest what is missing here.

 

nmrao
Champion Level 3


@Hellotest wrote:

I tried first with out datasource as below but getting null pointer error as attached. 

 

import static com.jayway.jsonpath.JsonPath.parse
def jsonString = context.expand( '${GetSources#Response}' )
def sourceId = new groovy.json.JsonSlurper().parseText(jsonString).result.find{it.SourceCode == 'ABC'}
log.info "source id: ${sourceId.SourceId}"

 


You almost there, but trivial errors.

 

 

 

//Get the parsed json of GetSources test step response
def json = new groovy.json.JsonSlurper().parseText(context('${GetSources#Response}'))

//Closure to the the SourceId for a given SourceCode of the json
def getSourceIdBySourceCode = { code ->  json.find {it?.SourceCode == code}?.SourceId }

//Check if the value is matching
assert 'b4035134-ca33-4b33-b3c7-06ea880f1f28' == getSourceIdBySourceCode( 'DEF')
assert 'bc3cef1e-a9f1-46df-a4f0-c1131357ea57' == getSourceIdBySourceCode( 'ABC')

 

 

 

You can test it online here

https://ideone.com/p8X57U



Regards,
Rao.

Thanks nmrao for your suggestions. It is working fine but failing in one condition.

 

example- In situation where XYZ is not present in JSON .

getSourceIdBySourceCode( 'XYZ')

 

XYZ is coming form my datasource but that value may not be present in JSON then getting null pointer.

here we are supposed to add new source but prior to it , need to check source id but getting java.lang.NullPointerException : cannot get property 'Sourceid' on null object.

cancel
Showing results for 
Search instead for 
Did you mean: