/**
* Below is script assertion
* for the Rest Request test step
* which reads json response
* and extract user requested data
* write into a csv file in system temporary directory
**/
def json = new groovy.json.JsonSlurper().parseText(context.response)
def promotions = json.personalizedOffers*.validCoupon.promotion
//You may add more columns if needed
def columns = ['createdTime', 'lastUpdatedTime', 'shortDescription', 'longDescription']
def sb = new StringBuffer()
sb.append(columns.join(',')).append('\n')
def buildPromotionData = { promotion ->
columns.eachWithIndex { key, index ->
promotion[key] ? sb.append(promotion[key]) : sb.append('')
(index+1 != columns.size()) ? sb.append(',') : sb.append('\n')
}
}
promotions.each { buildPromotionData(it) }
def file = "${System.getProperty('java.io.tmpdir')}/${System.currentTimeMillis()}.csv" as String
log.info "Creating the file ${file}"
new File(file).write(sb.toString())
log.info sb.toString()
The above Script Assertion does what was requested. No additional libraries or test steps required.
You may be able to open "CSV" file in excel if you want.