# Trying to pass boolean variable to JSON gets 'Expected type boolean but found type string' error on POST

**URL:** <https://community.postman.com/t/trying-to-pass-boolean-variable-to-json-gets-expected-type-boolean-but-found-type-string-error-on-post/1872>\
**Category:** Help Hub\
**Created:** [June 14, 2018, 3:30pm UTC](https://community.postman.com/t/trying-to-pass-boolean-variable-to-json-gets-expected-type-boolean-but-found-type-string-error-on-post/1872 "2018-06-14T15:30:39Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![ed\_truqc](https://avatars.discourse-cdn.com/v4/letter/e/ac91a4/32.png) [@ed\_truqc](https://community.postman.com/u/ed_truqc)\
**Post date:** [June 14, 2018, 3:30pm UTC](https://community.postman.com/t/trying-to-pass-boolean-variable-to-json-gets-expected-type-boolean-but-found-type-string-error-on-post/1872/1 "2018-06-14T15:30:39Z")

</div>

Hi there,

I’m trying to randomize a `true`/`false` boolean value, which I want to assign to a variable that I can then pass to my Body and post as part of my JSON. I tried this:

```auto
var arrayCert = [true, false];
pm.globals.set('certVal', arrayCert[Math.floor(Math.random()*arrayCert.length)]);

```

Which _does_ get me a random value in `certVal` . But when I try to pass that variable in my Body like so:

```auto
"Certified": "{{certVal}}",

```

I get this message:

```auto
"INVALID_REQUEST_PARAMETER: 'Certified' Expected type boolean but found type string; ",

```

I think the double quotes in the body is passing the variable as a string, hence the error. If I change it to:

```auto
"Certified": true,

```

Then the post works as expected. Is there anyway I can pass a random boolean in this way?

Thanks so much for any help.

Ed

---

<div class="post-metadata">

**Author:** ![gopikrishna4595](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/gopikrishna4595/32/514_2.png) [@gopikrishna4595](https://community.postman.com/u/gopikrishna4595)\
**Post date:** [June 15, 2018, 12:40pm UTC](https://community.postman.com/t/trying-to-pass-boolean-variable-to-json-gets-expected-type-boolean-but-found-type-string-error-on-post/1872/2 "2018-06-15T12:40:57Z")

</div>

@ed_truqc

as far as i know 🤔  
Boolean values weren’t denoted with double quotes.  
if the Boolean value is double quoted, it is considered as string. (same happened in your case).

you can try this to pass the Boolean.

`"Certified": {{certVal}},`

---

<div class="post-metadata">

**Author:** ![ed\_truqc](https://avatars.discourse-cdn.com/v4/letter/e/ac91a4/32.png) [@ed\_truqc](https://community.postman.com/u/ed_truqc)\
**Post date:** [June 15, 2018, 5:33pm UTC](https://community.postman.com/t/trying-to-pass-boolean-variable-to-json-gets-expected-type-boolean-but-found-type-string-error-on-post/1872/3 "2018-06-15T17:33:52Z")

</div>

@gopikrishna4595, thanks for your response. I have tried this, and it appears to be working when the value is true. But if the value if false, I get this error:

```auto
expected '{"error_msg":"Unexpected token , in JSON at position 109","error_debug":"DEBUG: ","error_type":""}' 

```

Any idea why `false` alone would cause this error?

I can actually see that when `certVal` is set to true, it gets passed as true:

```auto
\"Certified\": true,\n

```

but when `certVal` is set to false, it gets passed as a blank:

```auto
\"Certified\": ,\n

```

Thanks!  
Ed

---

<div class="post-metadata">

**Author:** ![gopikrishna4595](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/gopikrishna4595/32/514_2.png) [@gopikrishna4595](https://community.postman.com/u/gopikrishna4595)\
**Post date:** [June 15, 2018, 6:27pm UTC](https://community.postman.com/t/trying-to-pass-boolean-variable-to-json-gets-expected-type-boolean-but-found-type-string-error-on-post/1872/4 "2018-06-15T18:27:05Z")

</div>

@ed_truqc  
can you comment the tests and manually set certVal as false in Environments and execute ?

---

<div class="post-metadata">

**Author:** ![ed\_truqc](https://avatars.discourse-cdn.com/v4/letter/e/ac91a4/32.png) [@ed\_truqc](https://community.postman.com/u/ed_truqc)\
**Post date:** [June 15, 2018, 6:46pm UTC](https://community.postman.com/t/trying-to-pass-boolean-variable-to-json-gets-expected-type-boolean-but-found-type-string-error-on-post/1872/5 "2018-06-15T18:46:22Z")

</div>

@gopikrishna4595 . It used to work but now it’s getting an error. I’ll check with my devs as there may be something wrong with our api.

Gaaah, sorry for all the edits. So if I manually set `Certified` to false, the post works correctly. If I change my array to false only, rather than true and false, then `certVal` is set to false, but `Certified` is set to blank:

```auto
\"Certified\": ,\n

```

---

<div class="post-metadata">

**Author:** ![ed\_truqc](https://avatars.discourse-cdn.com/v4/letter/e/ac91a4/32.png) [@ed\_truqc](https://community.postman.com/u/ed_truqc)\
**Post date:** [June 15, 2018, 7:17pm UTC](https://community.postman.com/t/trying-to-pass-boolean-variable-to-json-gets-expected-type-boolean-but-found-type-string-error-on-post/1872/6 "2018-06-15T19:17:05Z")

</div>

@gopikrishna4595 Just re-read your post. Sorry for the confusion. I just tried setting certVal as an environment variable, set to false, and that worked!

```auto
{
      "key": "certVal",
      "value": "false",
      "enabled": true,
      "type": "text"
    },

```

I think I may have a problem with my api, as I’m seeing inconsistent results. Sometimes the post passes and sometimes it fails. I’ll investigate further on my end, but this does appear to be working now. Thanks again so much for your help and patience!

Ed

---

<div class="post-metadata">

**Author:** ![ed\_truqc](https://avatars.discourse-cdn.com/v4/letter/e/ac91a4/32.png) [@ed\_truqc](https://community.postman.com/u/ed_truqc)\
**Post date:** [June 17, 2018, 6:09pm UTC](https://community.postman.com/t/trying-to-pass-boolean-variable-to-json-gets-expected-type-boolean-but-found-type-string-error-on-post/1872/7 "2018-06-17T18:09:59Z")

</div>

OK, I’ve changed my code that randomly picks a true/false value to include quotes, and set my `certVal` variable to be an environment variable:

```auto
"var arrayCert = ['true', 'false'];",
"pm.environment.set('certVal', arrayCert[Math.floor(Math.random()*arrayCert.length)]);",

```

My `postman_environment.json` file now includes this:

```auto
{
      "key": "certVal",
      "value": "false",
      "enabled": true,
      "type": "text"
    },

```

And my POST body includes `certVal` in this format:

```auto
\"Certified\": {{certVal}},\n

```

And when I run my POST request, I do always get a true or false boolean passed as part of my post.

Thanks so much @gopikrishna4595 for the help on this!

Ed
