0
votes

Could anyone please guide me how do I construct a dynamic json http request body based on the # of ids that were stored in a list? Below is an example of required request body: sample request body I had a JSR223 script in place to collect IDs and store them in a list. Below is part of my script (post processor) to pass each id in the list to the http request body.

for (int j=0; j < myList.size(); j++) {
vars.put(“accountId”, qualifiedList[j])

}

And below is the structure of HTTP request body: HTTP request body

The issue I had was that with this configuration, only the last id in the list was passed to the body... So how do I construct a dynamic json payload that contains all ids as the example request body?

1

1 Answers

0
votes
  1. Each iteration of the for loop you're overwriting the accountId variable value with the new value from your qualifiedList
  2. You're supposed to produce a JSON Array and you're generating a single String

Suggested code change:

def payload = []

for (int j = 0; j < myList.size(); j++) {
    payload.add([accountId: qualifiedList[j]])
}

vars.put("accountId", new groovy.json.JsonBuilder(payload).toPrettyString())

More information: