# Checking dynamic variables in pre-request script

**URL:** <https://community.postman.com/t/checking-dynamic-variables-in-pre-request-script/30873>\
**Category:** Help Hub\
**Tags:** variables, collections, json\
**Created:** [December 20, 2021, 5:19pm UTC](https://community.postman.com/t/checking-dynamic-variables-in-pre-request-script/30873 "2021-12-20T17:19:35Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![rafall92](https://avatars.discourse-cdn.com/v4/letter/r/4bbf92/32.png) [@rafall92](https://community.postman.com/u/rafall92)\
**Post date:** [December 20, 2021, 5:19pm UTC](https://community.postman.com/t/checking-dynamic-variables-in-pre-request-script/30873/1 "2021-12-20T17:19:35Z")

</div>

Hello

I need to prepare Pre-request script. In my request i have dynamic Json body

```auto
{
    "name": {{name}},
    "template": {{template}}
}

```

This dynamic values are loaded from json file. I need to check in pre-request script vaule of curren request variable. How can I do that?

---

<div class="post-metadata">

**Author:** ![danny-dainton](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/danny-dainton/32/29401_2.png) [@danny-dainton](https://community.postman.com/u/danny-dainton)\
**Post date:** [December 20, 2021, 9:52pm UTC](https://community.postman.com/t/checking-dynamic-variables-in-pre-request-script/30873/2 "2021-12-20T21:52:56Z")

</div>

Hey @rafall92

If I understand you correctly, you should be able to use this to log the `name` variable to the console:

```auto
console.log(pm.iterationData.get('name'));

```

More about `iterationData` can be found here:

> **[Postman JavaScript reference](https://learning.postman.com/docs/writing-scripts/script-references/postman-sandbox-api-reference/#using-data-variables-in-scripts)**

---

<div class="post-metadata">

**Author:** ![rafall92](https://avatars.discourse-cdn.com/v4/letter/r/4bbf92/32.png) [@rafall92](https://community.postman.com/u/rafall92)\
**Post date:** [December 20, 2021, 10:38pm UTC](https://community.postman.com/t/checking-dynamic-variables-in-pre-request-script/30873/3 "2021-12-20T22:38:50Z")

</div>

Yes, that’s what i’m looking for. Technically I need that to check if data already exists(name and template are send via post request) In pre-request i write script, where I send Get request to recive all data from my Api, ant then compare it with one i obtain with iterationData.get. Is that right approach or can I do it better?

---

<div class="post-metadata">

**Author:** ![nrelkov](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/nrelkov/32/10495_2.png) [@nrelkov](https://community.postman.com/u/nrelkov)\
**Post date:** [December 20, 2021, 11:21pm UTC](https://community.postman.com/t/checking-dynamic-variables-in-pre-request-script/30873/4 "2021-12-20T23:21:18Z")

</div>

Hi @rafall92

If you want to have it determined if you need to run your Request, what I usually end up doing (not sure if there is a better way) is to create a Request that runs before the request that you use.

Example:

Request 1 (“DataCheck”):  
No Pre-Request script  
Have request use Get, if you have an endpoint that is a ping response, use that  
Tests  
if(pm.iterationData.get(‘name’) == “”) {  
postman.setNextRequest(null);  
} else {  
postman.setNextRequest(“MyRequest”);  
}

Request 2 (“MyRequest”):  
\*This is your script

Hope this helps!
