# Getting a JSON parse error when running a collection if there is a variable in the request body

**URL:** <https://community.postman.com/t/getting-a-json-parse-error-when-running-a-collection-if-there-is-a-variable-in-the-request-body/4245>\
**Category:** Help Hub\
**Tags:** collections\
**Created:** [February 21, 2019, 6:34pm UTC](https://community.postman.com/t/getting-a-json-parse-error-when-running-a-collection-if-there-is-a-variable-in-the-request-body/4245 "2019-02-21T18:34:10Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![maheshb123](https://avatars.discourse-cdn.com/v4/letter/m/47e85d/32.png) [@maheshb123](https://community.postman.com/u/maheshb123)\
**Post date:** [February 21, 2019, 6:34pm UTC](https://community.postman.com/t/getting-a-json-parse-error-when-running-a-collection-if-there-is-a-variable-in-the-request-body/4245/1 "2019-02-21T18:34:10Z")

</div>

Here is the body of my request. You will note the {{numProducts}} variable.  
{  
“query”: {  
“bool”: {  
“filter”: [  
{  
“term”: {  
“language”: “EN”  
}  
},  
{  
“term”: {  
“region”: “USA”  
}  
},  
{  
“term”: {  
“season”: “FA2019”  
}  
}  
]  
}  
},  
“size”:{{numProducts}},  
“\_source”: [  
“language”,  
“genderAge”,  
“division”,  
“productCode”,  
“region”  
]  
}  
I also have a pre-req script which has this line : var sources = JSON.parse(pm.request.body.raw).\_source.toString();

When I run the request manually, I do not have an issue, but when I run it as part of a collection, I get this error: JSONError: Unexpected token ‘n’ at 23:14 “size”:{{numProducts}}, ^

---

<div class="post-metadata">

**Author:** ![matt](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/matt/32/2562_2.png) [@matt](https://community.postman.com/u/matt)\
**Post date:** [February 22, 2019, 9:58pm UTC](https://community.postman.com/t/getting-a-json-parse-error-when-running-a-collection-if-there-is-a-variable-in-the-request-body/4245/2 "2019-02-22T21:58:02Z")

</div>

I think it’s going to be the case that variables in the `{{variable}}` notation are interpolated at runtime i.e. when the request is executed. As you’re working within the pre-request script, you’ll need to sort it out yourself if that’s what you want to do.

```auto
var requestBody = JSON.parse(pm.request.body).raw.replace('{{numProducts}}', pm.variables.get('numProducts'));

```

Will give you the body in string format, JSON.parse the entire thing if you want it as an object.
