# How to dynamically add variable both key&value into json raw body of request?

**URL:** <https://community.postman.com/t/how-to-dynamically-add-variable-both-key-value-into-json-raw-body-of-request/14675>\
**Category:** Help Hub\
**Tags:** variables\
**Created:** [August 3, 2020, 11:56am UTC](https://community.postman.com/t/how-to-dynamically-add-variable-both-key-value-into-json-raw-body-of-request/14675 "2020-08-03T11:56:09Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![x-ray-x](https://avatars.discourse-cdn.com/v4/letter/x/f04885/32.png) [@x-ray-x](https://community.postman.com/u/x-ray-x)\
**Post date:** [August 3, 2020, 11:56am UTC](https://community.postman.com/t/how-to-dynamically-add-variable-both-key-value-into-json-raw-body-of-request/14675/1 "2020-08-03T11:56:09Z")

</div>

Hi,  
I have two variables borrowName and Seller, I need to dynamically add variable both key&value into json raw body of request,

- case1. if borrowName is null, then I can add seller into request body.
- case2. if seller is null, then I can add borrowName into request body.

These two variables are not fixed and need to be removed/added dynamically according to the empty value.  
here is the capture,

 ![capture_2020-08-03_19-24-40](https://us1.discourse-cdn.com/getpostman/original/2X/6/6bc53cfbe1375a12bf6c138f356165c50e54400a.png) ,

I don’t know how to do this via postman pre-request script.

Any help will be greatly appreciated.

---

<div class="post-metadata">

**Author:** ![kevin.swiber](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/kevin.swiber/32/3820_2.png) [@kevin.swiber](https://community.postman.com/u/kevin.swiber)\
**Post date:** [August 3, 2020, 8:16pm UTC](https://community.postman.com/t/how-to-dynamically-add-variable-both-key-value-into-json-raw-body-of-request/14675/2 "2020-08-03T20:16:07Z")

</div>

Hi @x-ray-x,

You can either use a variable to capture the entire JSON document and include something like `{{dynamicPayload}}` as the raw request body or you can try to make just a piece of the JSON request body dynamic. Here’s an example.

Pre-request script:

```auto
const borrowerName = pm.variables.get('borrowerName');
const seller = pm.variables.get('seller');

const key = 'borrowerNameAndSellerResolved';
if (borrowerName && borrowerName !== "null") {
    pm.variables.set(key, `,"borrowerName": "${borrowerName}"`);
} else if (seller && seller !== "null") {
    pm.variables.set(key, `,"seller": "${seller}"`);
}

```

Request body:

```auto
{
    "loan": {
        "productName": "product"
        {{borrowerNameAndSellerResolved}}
    }
}

```

Hope that helps.

Best,

Kevin

---

<div class="post-metadata">

**Author:** ![x-ray-x](https://avatars.discourse-cdn.com/v4/letter/x/f04885/32.png) [@x-ray-x](https://community.postman.com/u/x-ray-x)\
**Post date:** [August 4, 2020, 2:58am UTC](https://community.postman.com/t/how-to-dynamically-add-variable-both-key-value-into-json-raw-body-of-request/14675/3 "2020-08-04T02:58:58Z")

</div>

Hi @kevin.swiber ,  
This is a really big help 💪, it works, thank you.

 ![image](https://us1.discourse-cdn.com/getpostman/original/2X/c/cb4634b21e6dd25a1f59cad18cf2b927245124f0.png)  
Also, does postman have a detailed script reference doc, I want to learn more about it.
