0
votes

I'm a new on Jmeter, I'm using JSR223 PostProcessor to get some properties in the response. Unfortunately, my response is returned as a string json as below:

"{\n "trueOdds": "1.9047619047619047",\n "displayOdds": "1.90",\n "minStake": 50.0,\n "maxStake": 105263,\n "selectionId": "11318855000001015h",\n "oddsStyle": "de",\n "offerId": "1911208285033005"\n}"

Now I want to get trueOdds, displayOdds for using in the next request. I tried to use following code to get them:

import groovy.json.JsonSlurper

def jsonString = prev.getResponseDataAsString();

def jsonSlurper = new JsonSlurper();

def object = jsonSlurper.parseText(jsonString);

vars.put("trueOdds", object.trueOdds);

vars.put("displayOdds", object.displayOdds);

But I get following error:

ERROR o.a.j.e.JSR223PostProcessor: Problem in JSR223 script, JSR223 PostProcessor javax.script.ScriptException: groovy.lang.MissingPropertyException: No such property: trueOdds for class: java.lang.String

Can anyone support to resolve this? thanks a lot.

1

1 Answers

0
votes

I cannot reproduce your issue given the following JSON payload:

{
  "trueOdds": "1.9047619047619047",
  "displayOdds": "1.90",
  "minStake": 50.0,
  "maxStake": 105263,
  "selectionId": "11318855000001015h",
  "oddsStyle": "de",
  "offerId": "1911208285033005"
}

Demo:

enter image description here

If your response looks exactly how you show it to us it's not a valid JSON therefore you need:

  1. Remove first and last " via i.e. substring function
  2. Remove all occurrences of \n via i.e. replaceAll function

Suggested code amendment:

import groovy.json.JsonSlurper

def jsonString = prev.getResponseDataAsString();

def withoutQuotationMarks = jsonString.substring(1, jsonString.length() - 1)
def withoutLineBreaks = withoutQuotationMarks.replaceAll('\\\\n','')

def jsonSlurper = new JsonSlurper();

def object = jsonSlurper.parseText(withoutLineBreaks);

vars.put("trueOdds", object.trueOdds);

vars.put("displayOdds", object.displayOdds);

More information on Groovy scripting in JMeter: Apache Groovy - Why and How You Should Use It