I have seen several posts related to this, but I couldn't find a close match to my scenario, hence posting it as a new question.
I have a below JSON response
{
"myshop": [
{
"url": "test1",
"hits": 1000
},
{
"url": "test2",
"hits": 2000
},
{
"url": "test3",
"hits": 3000
}
]
}
I would like to extract each of URL, its corresponding hits, and store it in a CSV file, in this case, it would be 3 lines in the CSV file
test1,1000
test2,2000
test3,3000
but couldn't succeed in doing this. My JSON extractor:
$..url;$..hits with match number as -1
but it picks all the URLs but any random value of hits. Also, I was able to use a for each controller with one variable to extract from JSON, write it to CSV using BeanShell post processor successfully but the same doesn't work for this case. Any pointers would be helpful. I did check there were few answers with JSR223 Post processor but that did not cover this problem scenario.
Thanks in advance
Put the following code into "Script" area:
new groovy.json.JsonSlurper().parse(prev.getResponseData()).myshop.each { entry ->
new File('entries.csv') << entry.get('url') << ',' << entry.get('hits') << System.getProperty('line.separator')
}
That's it, once you run your test entries.csv
file will be generated in JMeter's "bin" folder containing the information in the required format.
References:
Also be aware that the approach will reliably work given the code is executed with maximum 1 thread at a time (i.e. make sure to use Critical Section Controller). Otherwise consider switching to Sample Variables and Flexible File Writer