Reputation: 7
I have csv
name,capital,zone,code,address,city,zip,state,phone,email
Test City,false,America,us,Address line 2,New York,10002,NY,(+1) 111-111-1112,[email protected]
What I do
import groovy.json.JsonBuilder
def json = new JsonBuilder()
json {
name vars.get("name")
capital vars.get("capital")
zone vars.get("zone")
code vars.get("code")
address vars.get("address")
city vars.get("city")
zip vars.get("zip")
state vars.get("state")
phone vars.get("phone")
email vars.get("email")
}
sampler.addNonEncodedArgument("",json.toPrettyString(),"")
sampler.setPostBodyRaw(true)
But csv could consist with multiple amount of rows and not every time a need all rows from csv
So, I need some variable which will be set some counter for loop, read counter=rows from csv and return me JSON
Try to found solution here, but always have troubles with syntax while Im noob in Groovy(
As result, it should be 1 request, 1 payload, with multiple items=rows from csv
Upvotes: 0
Views: 1203
Reputation: 527
You need to grab the CSV field names, from the header, then iterate through the CSV data lines.
In the exampe below I convert each line to a map then push the map into a list. i.e. the list will contain one map for each non-header row. If you have 10 lines + a header then the list will contain 10 maps.
After that it's easy, as JsonBuilder can create Json from a list of maps.
import groovy.json.JsonBuilder
File csv = new File( 'ParseCsvAsJson.csv')
def lines = csv.readLines()
def header = lines[0]
def fieldNames = header.split(/,[ ]*/)
def mappedLines = []
lines[1..-1].each { line ->
def mappedLine = [:]
def fields = line.split(/,[ ]*/)
fields.eachWithIndex { value, index ->
String name = fieldNames[ index ]
mappedLine[ name ] = value
}
mappedLines << mappedLine
}
def builder = new JsonBuilder()
builder {
json builder( mappedLines )
}
println builder.toPrettyString()
Upvotes: 0
Reputation: 168157
You won't be able to achieve this using CSV Data Set Config, you will have to parse the CSV file programmatically, something like:
def rows = 2 // how many rows you need to collect from the CSV
def entries = []
1.upto(rows, { index ->
def values = new File('/path/to/your/file.csv').readLines().get(index).split(',')
def entry = [:]
entry.put('name', values[0])
entry.put('capital', values[1])
entry.put('zone', values[2])
entry.put('code', values[3])
entry.put('address', values[4])
entry.put('city', values[5])
entry.put('zip', values[6])
entry.put('state', values[7])
entry.put('phone', values[8])
entry.put('email', values[9])
entries.add(entry)
})
def json = new groovy.json.JsonBuilder(entries)
More information:
Upvotes: 1