# Conditional JSON payload based on csv variables

**URL:** <https://community.postman.com/t/conditional-json-payload-based-on-csv-variables/10886>\
**Category:** Help Hub\
**Tags:** variables\
**Created:** [February 19, 2020, 8:22am UTC](https://community.postman.com/t/conditional-json-payload-based-on-csv-variables/10886 "2020-02-19T08:22:49Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![mrosi](https://avatars.discourse-cdn.com/v4/letter/m/e0b2c6/32.png) [@mrosi](https://community.postman.com/u/mrosi)\
**Post date:** [February 19, 2020, 8:22am UTC](https://community.postman.com/t/conditional-json-payload-based-on-csv-variables/10886/1 "2020-02-19T08:22:49Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![singhsivcan](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/singhsivcan/32/1595_2.png) [@singhsivcan](https://community.postman.com/u/singhsivcan)\
**Post date:** [February 19, 2020, 11:07am UTC](https://community.postman.com/t/conditional-json-payload-based-on-csv-variables/10886/2 "2020-02-19T11:07:49Z")

</div>

@mrosi Welcome to the community! 👋

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

---

<div class="post-metadata">

**Author:** ![mrosi](https://avatars.discourse-cdn.com/v4/letter/m/e0b2c6/32.png) [@mrosi](https://community.postman.com/u/mrosi)\
**Post date:** [February 19, 2020, 11:20am UTC](https://community.postman.com/t/conditional-json-payload-based-on-csv-variables/10886/3 "2020-02-19T11:20:24Z")

</div>

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

Thanks for your time

---

<div class="post-metadata">

**Author:** ![singhsivcan](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/singhsivcan/32/1595_2.png) [@singhsivcan](https://community.postman.com/u/singhsivcan)\
**Post date:** [February 19, 2020, 5:29pm UTC](https://community.postman.com/t/conditional-json-payload-based-on-csv-variables/10886/4 "2020-02-19T17:29:38Z")

</div>

@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](https://learning.postman.com/docs/postman/scripts/postman-sandbox-api-reference/#pmiterationdata) in your **pre-request script** :

```javascript
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](https://learning.postman.com/docs/postman/variables-and-environments/variables/#variable-scopes).

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.

---

<div class="post-metadata">

**Author:** ![mrosi](https://avatars.discourse-cdn.com/v4/letter/m/e0b2c6/32.png) [@mrosi](https://community.postman.com/u/mrosi)\
**Post date:** [February 19, 2020, 5:49pm UTC](https://community.postman.com/t/conditional-json-payload-based-on-csv-variables/10886/5 "2020-02-19T17:49:19Z")

</div>

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