0

In my post request create a job whose response have only id as follow,

{"id":626}

And I want to save the id value which is 626 in a csv or any file and after my test complete I want use all these value from this file to check the status of the job in the tear down thread group.

how to complete this ? I have following script but getting error,

new groovy.json.JsonSlurper().parse(prev.getResponseData()).id.each { entry ->
    new File('result.csv') << entry.get('id') << System.getProperty('line.separator')
}

error details,

2021-01-15 12:13:05,699 ERROR o.a.j.e.JSR223PostProcessor: Problem in JSR223 script, JSR223 PostProcessor
javax.script.ScriptException: groovy.lang.MissingMethodException: No signature of method: java.lang.Integer.get() is applicable for argument types: (String) values: [id]
Possible solutions: getAt(java.lang.String), next(), grep(), grep(java.lang.Object), wait(), abs()

2 Answers2

0

Just change entry.get('id') to just entry and it should work:

new groovy.json.JsonSlurper().parse(prev.getResponseData()).id.each { entry ->
    new File('result.csv') << entry << System.getProperty('line.separator')
}

However there is a potential problem with your approach, if you run your script with > 1 thread you may run into a race condition when multiple threads will be concurrently writing into the same file so more error-proof approach would be:

  1. Add the next line to user.properties file:

    sample_variables=id
    
  2. Extract the id from the response using JSON JMESPath Extractor configured like:

    enter image description here

  3. Use Flexible File Writer to write the value(s) to the file:

    enter image description here

Dmitri T
  • 159,985
  • 5
  • 83
  • 133
  • Dmitri, yeah the second option is more viable, one quick question suppose I have multiple step and and out of few are generating this job id individual each, then can be same file be used for the same condition. –  Jan 15 '21 at 09:04
0

Yeah, agree with Dmitri prefer the second option, and just the blow line should fix your issue.

new groovy.json.JsonSlurper().parse(prev.getResponseData()).id.each { entry ->
    new File('result.csv') << entry << System.getProperty('line.separator')
}

Like Dmitri mentioned.

Jyoti Prakash Mallick
  • 2,119
  • 3
  • 21
  • 38