Reputation: 1566
Background:
I have an API that returns a response like so:
{
"status": 1,
"errorCode": null,
"message": null,
"data": [
{
"id": 33,
"snapshotId": 2,
"ceId": 29,
"month": "Feb",
"corpRcvPayAmt": 100000,
"wthRcvPayAmt": -90000
},
{
"id": 31,
"snapshotId": 2,
"ceId": 29,
"month": "Jan",
"corpRcvPayAmt": 0,
"wthRcvPayAmt": 0
}
]
}
The data
node can vary from 0 size to 12 (empty to 1 per month). The only constant field for data
is the "month"
node. Other field values are changing.
Goal:
I want to do either a PUT
or a POST
request, depending on whether or not a specific month is already present in data
.
For this, I'm capturing the entire data
field into a variable using a Regular Expression Extractor like this:
With the sample response above, calling ${data}
will give me {"id":33,"snapshotId":2,"ceId":29,"month":"Feb","corpRcvPayAmt":100000.00,"wthRcvPayAmt":-90000.00},{"id":31,"snapshotId":2,"ceId":29,"month":"Jan","corpRcvPayAmt":0.00,"wthRcvPayAmt":0.00}
Now using this field, I just want to do a simple if condition like so: if ${data}
contains ${month}
do something (PUT request). I'm currently trying to do it like this:
Problem:
The condition ${__groovy("${data}".contains("Jan"))}
does not seem to work.
I've already tried doing the following:
${__groovy("${month}".contains("Jan"))}
just to see if the syntax is correct. This works just fine.
${__groovy("${month}".contains("Jan"))}
to print ${data}
. This also prints the data as expected.With this I can confirm the following:
${month}
and ${data}
are extracted and stored properly
${__groovy("${data}".contains("Jan"))}
is not working.
What am I missing here? Thanks in advance.
Upvotes: 3
Views: 11331
Reputation: 168217
Don't reference JMeter Functions and/or Variables in Groovy scripts as:
So instead of using ${data}
go for vars.get("data")
instead, vars
is a shorthand for JMeterVariables class instance and this is the best way of getting a JMeter Variable value from the Groovy script.
The whole function should look like:
${__groovy(vars.get('data').contains("Jan"),)}
Also be aware that it's better to use JSON Extractor for obtaining values from JSON responses.
Upvotes: 7