jmeterjsr223json-extractpost-processor

JMeter- extract 'id' variables from JSON response and save it in CSV file and use it in tear down thread


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()

Solution

  • 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