Home > Blockchain >  Unable to create correct JSON against csv file in Groovy within JSR Preprocessor
Unable to create correct JSON against csv file in Groovy within JSR Preprocessor

Time:01-19

Complete noob in JMeter.Been trying create a json using CSV file with Groovy and pass the same in the request body in jmeter but unable to create the JSON in required format:

Sample2.csv

Work Registration - External,3423,115

Jmeter HTTPRequest --JSR223Preprocessor

For above csv, the following code in JSR223Preprocessor provides output

import groovy.json.*
def builder = new groovy.json.JsonBuilder()

@groovy.transform.Immutable
class WKList {
    String wkname
    int Wkid
    int numId
}

def WKLists = new File("Sample2.csv")
        .readLines()
        .collect { line ->
            new WKList(Integer.parseInt(line.split(",")[0]), Integer.parseInt(line.split(",")[1])),line.split(",")[2] }

builder(
        Place:'RA',
        Serial: 2,
        SerialDate: 'Dec 30 ,2021',
        WKList: WKLists.collect(),

)
log.info(builder.toPrettyString())

Output: { "Place": "RA", "Serial": 2, "SerialDate": "Dec 30 ,2021", "WKList": [ { "wkid": 3423, "numId": 115, "wkname": "Work Registration - External" } ] }

First 3 columns are hardcoded. But if original csv file is as follows

RA,8,Dec 30, 2021,Work Registration - External,3423,233

KA,92,Dec 20, 2021,State Registration - Internal,121,3

How can the code be modified to include values from csv rather than fetching hard coded values as done earlier ? Also if CSV Data Set Config JMeter is used, does it read per line for each user for each iteration ?

CodePudding user response:

This line looks very suspicious:

new WKList(Integer.parseInt(line.split(",")[0]), Integer.parseInt(line.split(",")[1])),line.split(",")[2] }

Your WKList class takes a String as a first parameter followed by 2 Integers

So my expectation is that you should change it to something like:

new WKList(line.split(",")[0], Integer.parseInt(line.split(",")[1]), Integer.parseInt(line.split(",")[2]))

Also your code assumes the following CSV file structure:

Work Registration - External,3423,115

If you need to include use the following file format:

RA,8,Dec 30, 2021,Work Registration - External,3423,233

we need to know what is the "required format" of the JSON.

In the meantime you can get familiarized with the following material:

CodePudding user response:

This would work for csv file line RA,8,"Dec 30, 2021",Work Registration - External,3423,233

import groovy.json.*

class WKList {String wkname; int Wkid ;int numId}
List<WKList> WKDetList = new ArrayList<>();
def lines =new File("Sample2.csv").readLines()

def content = [:]
def articles = []
def article = [:]
for (int i=1;i<lines.size();i  )

{ 
String[] splitData = lines[i].split(',');

content.put('Place', splitData[0])
content.put('Serial', splitData[1])
content.put('SerialDate', splitData[2])
article.put('wkname', splitData[3])
article.put('wkid', splitData[4])
article.put('numId', splitData[5])
articles.add(article)
content.put('WKList', articles)   
  
}
sampler.addNonEncodedArgument('', new groovy.json.JsonBuilder(content).toPrettyString(), '')
sampler.setPostBodyRaw(true)


log.info(JsonOutput.prettyPrint(JsonOutput.toJson(content)))
  •  Tags:  
  • Related