Conditional JSON payload based on csv variables

Hi everybody,

I’m quite new with POSTMAN so sorry if the question can be a little “neif” but i’ve this problem and i don’t know how to fix it.

Basically i’m using .csv files to collect variables that then will be used by postman POSTs to populate {{…}} items inside the JSON payload.

Depending on the value of csv fields, i can have or not piece of JSON code included in the post.

Just with an example:

Let’s suppose to have the csv done like this one:

VRF_NAME,BD_NAME,BD_NAME_ALIAS,IP_ANYCAST,L3OUT_ASSOCIATION
IRIDEOS_VRF,IRIDEOS_BD1,IRIDEOS_BD1_ALIAS, ,IRIDEOS_L3OUT
IRIDEOS_VRF,IRIDEOS_BD2,IRIDEOS_BD2_ALIAS,10.2.1.1/24,
IRIDEOS_VRF,IRIDEOS_BD3,IRIDEOS_BD3_ALIAS,10.3.1.1/24,IRIDEOS_L3OUT

As you can see, in the 2nd line is missing the value for the “IP_ANYCAST” variable and in the 3rd line is missing the value for the “L3OUT_ASSOCIATION” variable.

Now, the POST call in JSON is like this one:

{
“fvBD”: {
“attributes”: {
“dn”: “uni/tn-{{TENANT_NAME}}/BD-{{BD_NAME}}”,
“mac”: “00:22:BD:F8:19:FF”,
“arpFlood”: “true”,
“name”: “{{BD_NAME}}”,
“nameAlias”: “{{BD_NAME_ALIAS}}”,
“rn”: “BD-{{BD_NAME}}”,
“unicastRoute”: “true”,
“unkMacUcastAct”: “flood”,
“status”: “created”
},
“children”: [
{
“fvSubnet”: {
“attributes”: {
“dn”: “uni/tn-{{TENANT_NAME}}/BD-{{BD_NAME}}/subnet-[{{IP_ANYCAST}}]”,
“ctrl”: “”,
“ip”: “{{IP_ANYCAST}}”,
“rn”: “subnet-[{{IP_ANYCAST}}]”,
“status”: “created”
},
“children”:
}
},
{
“fvRsCtx”: {
“attributes”: {
“tnFvCtxName”: “{{VRF_NAME}}”,
“status”: “created,modified”
},
“children”:
}
},
{
“fvRsBDToOut”: {
“attributes”: {
“tnL3extOutName”: “{{L3OUT_ASSOCIATION}}”,
“status”: “created”
},
“children”:
}
}
]
}
}

Want i wont to get as outcome is:

  1. Not include the portion below for the line where “IP_ANYCAST” is empty (or null)

{
“fvSubnet”: {
“attributes”: {
“dn”: “uni/tn-{{TENANT_NAME}}/BD-{{BD_NAME}}/subnet-[{{IP_ANYCAST}}]”,
“ctrl”: “”,
“ip”: “{{IP_ANYCAST}}”,
“rn”: “subnet-[{{IP_ANYCAST}}]”,
“status”: “created”
},
“children”:
}
}

  1. not include the portion below for the line where “L3OUT_ASSOCIATION” is empty (or null)

{
“fvRsBDToOut”: {
“attributes”: {
“tnL3extOutName”: “{{L3OUT_ASSOCIATION}}”,
“status”: “created”
},
“children”:
}
}

So in summary, i need to make the JSON payload dynamically dependent on some variables. How can i get that?

I really appreciate your feedback.
Thanks
Mario

@mrosi Welcome to the community! :wave:

Before we get into solving this issue, just to be sure - you’re using this in collection runner right?

Hi Sivcan,
yes i’m using in collection runner!

Thanks for your time

@mrosi
Here’s what you can do:

  1. Build a dynamic request body, what I mean by that is you need to build the request body from the pre-request script, and make it a variable something like this:

  2. So every iteration in the runner will get the corresponding data from the csv file.
    To access the current iteration data you can use the pm.iterationData API in your pre-request script:

let currentData = pm.iterationData.toJSON(),
 dynamicRequestBody = {};

// Now let's build the body using conditionals etc:
if (currentData.IP_ANYCAST) {
  dynamicRequestBody = currentData;
}
else {
  dynamicRequestBody = { name: currentData.NAME };
}

pm.variables.set('dynamicRequestBody', JSON.stringify(dynamicRequestBody));

This is just an example on how you can dynamically build the request body using the pre-request script with the help of local variables.

Now, based on your conditions you can build this body and store it as a local variable and during the request execution, this variable will get resolved and thus you can make the API call based on your CSV data.

1 Like

many thanks,
i’ll try to reproduce what you are suggesting.

1 Like