0

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

2
  • where is the question? Commented Jul 27, 2020 at 15:59
  • @daggett In existed example of code need to add some variable which will be set some counter for loop, read counter=rows from csv and return me JSON. As result, it should be 1 request, 1 payload, with multiple items=rows from csv Commented Jul 27, 2020 at 16:04

2 Answers 2

1

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:

Sign up to request clarification or add additional context in comments.

3 Comments

Because if I put this code in JSR223 PreProcessor, request payload is empty
I don't know where you copied and pasted "your" code from, but if you're going to use my - you need to copy paste sampler.addNonEncodedArgument("",json.toPrettyString(),"") and sampler.setPostBodyRaw(true) to the bottom as well. If there are still problems - check jmeter.log file for any Groovy-related errors
Tnx u so mush, now it works for me)) One more question, if I will need to prepare array in array, for example, { "name": John, "surname":Smith, "address": [ "city": New York, "street": first street ] ... } Could u pls provide example of entry.put format?
0

Convert the CSV to a list of maps & pass it to JsonBuilder

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.