# 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:** 1\
**Showing post:** 4

<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.

---

_[View the full topic](https://community.postman.com/t/conditional-json-payload-based-on-csv-variables/10886)._
