-1

I'm trying to generate JSON body dynamically using values in csv file. For this i'm making use of JSR223 PreProcessor with groovy script. I'm expecting the below format to be generate when i run the groovy script

    {
    "transactionId": "100",
    "lineItems": [{
            "lineItemNo": "1",
            "cardInfo": {
                "cardNumber": "3456"
            }
        },
        {
            "lineItemNo": "2",
            "cardInfo": {
                "cardNumber": "45698"
            }
        }
    ]
}

but when i execute script i'm getting below format

POST data:
{
    "transactionId": "100",
    "lineItems": [
        {
            "lineItemNo": "1",
            "Cardinfo": [
                9255000012794606,
                9255000012794645
            ]
        },
        {
            "lineItemNo": "1",
            "Cardinfo": [
                9255000012794606,
                9255000012794645
            ]
        }
    ]
}

Script to generate json body

File csvFile = new File("D:\\Project Related Docs\\Jmeter\\apache-jmeter-5.0\\bin\\Map_Performance\\Map_New_Auto_jmx\\2Cards.csv")
def cards = csvFile.readLines()
List<String> cardnumbmer = new ArrayList<>()
def counter = 1
cards.eachWithIndex{line,idx->cardnumbmer.add(line)}
log.info("size of csv = "+cardnumbmer.size())
log.info("File conents = "+cardnumbmer[0])

//build the json body
def ids = new groovy.json.JsonSlurper().parseText(cardnumbmer.toString())
log.info("cardnumbmer to string = "+cardnumbmer.toString())
def builder = new groovy.json.JsonBuilder()

builder([
    transactionId:"100",
    lineItems:ids.collect{[lineItemNo:"1",Cardinfo: ids.collect{carnumber: it}]}
    ])

//sampler.getArguments().removeAllArguments()
sampler.addNonEncodedArgument('',builder.toPrettyString(),'')
sampler.setPostBodyRaw(true);

--CSV FILE have cardnumbers listed in row-wise look like below 9255000012794606 9255000012794645

Request to help me to know how to fix this issue.

Praveen PS
  • 127
  • 2
  • 5
  • 17

2 Answers2

1

ids.collect{carnumber: it} is basically ids. Be explicit about returning the map: ids.collect{ [carnumber: it] }

cfrick
  • 35,203
  • 6
  • 56
  • 68
  • Updated to return map and explicitly and made a little tweak in the builder code and it solved the issue - builder([ transactionId:"100", lineItems:ids.collect{[lineItemNo:"1",Cardinfo: [Cardnumber:it]]} ]) output is as expected – Praveen PS Apr 02 '20 at 06:38
0

Below code resolved the problem

//build the json body
def ids = new groovy.json.JsonSlurper().parseText(cardnumbmer.toString())
log.info("cardnumbmer to string = "+cardnumbmer.toString())
def builder = new groovy.json.JsonBuilder()
def count = 1

builder([
    transactionId:"100",
    //lineItems:ids.collect{[lineItemNo:"1",Cardinfo: count.collect{[carnumber: it]}]}
    lineItems:ids.collect{[lineItemNo:count++,Cardinfo: [Cardnumber:it]]}
    ])

//sampler.getArguments().removeAllArguments()
sampler.addNonEncodedArgument('',builder.toPrettyString(),'')
sampler.setPostBodyRaw(true);
Praveen PS
  • 127
  • 2
  • 5
  • 17
  • We can also make use of this solution https://stackoverflow.com/questions/41989897/concatenating-responses-in-jmeter – Praveen PS Apr 03 '20 at 12:48