Search code examples
groovyjmeterpreprocessor

JSR223 PreProcessor generates invalid format using Groovy in Jmeter


My Groovy script looks like

def statuses = []
def item = []
def items = []
def payload = []

1.upto(1, { index ->
    def data = new File('/Users/my/Desktop/items.csv').readLines().get(index).split(',')
    def a = [:]
        a.put('1', 5)
    a.put('2', 4)
    statuses.add(a)
    def attr = [:]
    attr.put('3', data[3].toInteger())
    def attributes = [:]
    attributes.put('4', statuses)
    attributes.put('5', attr)
    item.add(attributes)
})

1.upto(1, { index ->
    def data = new File('/Users/my/Desktop/locations.csv').readLines().get(index).split(',')
    def attr = [:]
    attr.put('6', item)
    attr.put('7', data[1].toInteger())
    items.add(attr)
    def attributes = [:]
    attributes.put('param', items)
    payload.add(attributes)

})

def json = new groovy.json.JsonBuilder(payload)

sampler.addNonEncodedArgument("",json.toPrettyString(),"")
sampler.setPostBodyRaw(true)

Result is payload for my http request

[
    {
        "param": [
            {
                "6": [
                    {
                        "4": [
                            {
                                "1": 5,
                                "2": 4
                            }
                        ],
                        "5": {
                            "3": 101
                        }
                    }
                ],
                "7": 20
            }
        ]
    }
]

The problem is when I prepare JsonBuilder(payload), def payload is array (def payload = []) As result I have those format of nesting

[
    {
        "param"

What should I do, to make my payload nesting without [] bracers? Like this:

{
   "param"
    ...


Solution

    1. def payload = [:]
    2. payload.put('param', your_request_body)

    You need to use a LazyMap in order to generate a JSON Object, not a JSON Array.

    More information: