0

hope you all are doing well.

I need to create a complex request body. My api call will take different email addresses from a CSV file and send a email notification to the listed email addresses in the request body. But the number of the email addresses will change based on the input provided by the user. My requirements are like this:

  • CSV data that contains different email infos. (I have that)
  • The number of email addresses in the request body will be between 10 - 1000 (I thought about using a user defined variable for that)
  • All the email addressed will be listed in the request body.
  • An incremental id will be assigned to all email addresses.

So the structure of my request body should be like this:

"message_job_id": "28b0a005-9ef1-475c-b33c-ade900f19e4c",
"campaign_group": "Entegrasyon",
"template_id": "cf585c8c-c675-40d2-b88a-ade900c898d5",
"recipient_list": [
{
"customer_id":"${target_id1}",
"target":{ "address": "${email1}" }
},
{
"customer_id":"${target_id2}",
"target":{ "address": "${email2}" }
},
{
"customer_id":"${target_id3}",
"target":{ "address": "${email3}" }
},
{
"customer_id":"${target_id4}",
"target":{ "address": "${email4}" }
},
..........
{
"customer_id":"${target_idX}",
"target":{ "address": "${emailX}" }
}

X will be defined by the user input.

Any idea how can I do that?

Thanks...


My CSV sample:

Reading CSV successfully finished, 20 records found:
${id} = 1000011
${email} = a1000011@smartmessage.com
------------
${id} = 1000012
${email} = a1000012@smartmessage.com
------------
${id} = 1000013
${email} = a1000013@smartmessage.com

My output payload:

"message_job_id": "28b0a005-9ef1-475c-b33c-ade900f19e4c",
    "campaign_group": "Entegrasyon",
    "template_id": "cf585c8c-c675-40d2-b88a-ade900c898d5",
    "recipient_list": [
        {
            "customer_id": null,
            "target": {
                "target": {
                    "address": null
                }
            }
        },
        {
            "customer_id": null,
            "target": {
                "target": {
                    "address": null
                }
            }
        },
        {
            "customer_id": null,
            "target": {
                "target": {
                    "address": null
                }
            }
        },
        {
            "customer_id": null,
            "target": {
                "target": {
                    "address": null
                }
            }
        },
        {
            "customer_id": null,
            "target": {
                "target": {
                    "address": null
                }
            }
        },
        {
            "customer_id": null,
            "target": {
                "target": {
                    "address": null
                }
            }
        },
        {
            "customer_id": null,
            "target": {
                "target": {
                    "address": null
                }
            }
        },
        {
            "customer_id": null,
            "target": {
                "target": {
                    "address": null
                }
            }
        },
        {
            "customer_id": null,
            "target": {
                "target": {
                    "address": null
                }
            }
        },
        {
            "customer_id": null,
            "target": {
                "target": {
                    "address": null
                }
            }
        }
    ]
}
Gökhan Uçar
  • 191
  • 15

1 Answers1

0

Most probably you're looking for JsonBuilder class

Example code would be something like:

def payload = [:]
def X = 1 // read user input here somehow

payload.put('message_job_id', '28b0a005-9ef1-475c-b33c-ade900f19e4c')
payload.put('campaign_group', 'Entegrasyon')
payload.put('template_id', 'cf585c8c-c675-40d2-b88a-ade900c898d5')

def recipient_list = []
1.upto(X, { x ->
    def recipient = [:]
    recipient.put('customer_id', vars.get('target_id' + x))
    def target = [:]
    def address = ['address': vars.get('email' + x)]
    target.put('target', address)
    recipient.put('target', target)
    recipient_list.add(recipient)
})

payload.put('recipient_list', recipient_list)


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

You should be able to refer generated request as ${payload} where required.

More information:

Dmitri T
  • 159,985
  • 5
  • 83
  • 133
  • Thanks a lot for your help! def X works like a charm! But I cannot get dynamic values from CSV file with your script. I updated my question. Could you please check that? – Gökhan Uçar Dec 27 '21 at 12:32