# Add an object to existing json

**URL:** <https://community.postman.com/t/add-an-object-to-existing-json/19728>\
**Category:** Help Hub\
**Tags:** tests\
**Created:** [January 29, 2021, 9:47pm UTC](https://community.postman.com/t/add-an-object-to-existing-json/19728 "2021-01-29T21:47:23Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![baljindernohra](https://avatars.discourse-cdn.com/v4/letter/b/e274bd/32.png) [@baljindernohra](https://community.postman.com/u/baljindernohra)\
**Post date:** [January 29, 2021, 9:47pm UTC](https://community.postman.com/t/add-an-object-to-existing-json/19728/1 "2021-01-29T21:47:23Z")

</div>

I want to add an object(key value pair) to a payload that i am getting from the external data file.

“payload”:"{“key1”:“value1”,“key2”:“value2”,“key3”:“value3”}"

What I want to do is add (“key4”:“value4”) to the payload that I use in request body.

---

<div class="post-metadata">

**Author:** ![praveendvd](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/praveendvd/32/6796_2.png) [@praveendvd](https://community.postman.com/u/praveendvd)\
**Post date:** [January 29, 2021, 11:20pm UTC](https://community.postman.com/t/add-an-object-to-existing-json/19728/2 "2021-01-29T23:20:19Z")

</div>

> [@baljindernohra](#):
>
> “payload”:“{“key1”:“value1”,“key2”:“value2”,“key3”:“value3”}”

```
payload = {“key1”:“value1”,“key2”:“value2”,“key3”:“value3”}

payload.key4 = value4

```

---

<div class="post-metadata">

**Author:** ![baljindernohra](https://avatars.discourse-cdn.com/v4/letter/b/e274bd/32.png) [@baljindernohra](https://community.postman.com/u/baljindernohra)\
**Post date:** [January 30, 2021, 5:03am UTC](https://community.postman.com/t/add-an-object-to-existing-json/19728/3 "2021-01-30T05:03:23Z")

</div>

how would i use that? I mean lets suppose the payload(from external data file) is put in the body as

{  
“branchPayloadDTO”: {{payload}}  
}

How would i add another key value pair to that payload in request body?

---

<div class="post-metadata">

**Author:** ![praveendvd](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/praveendvd/32/6796_2.png) [@praveendvd](https://community.postman.com/u/praveendvd)\
**Post date:** [January 30, 2021, 10:50am UTC](https://community.postman.com/t/add-an-object-to-existing-json/19728/4 "2021-01-30T10:50:04Z")

</div>

```
payload = {“key1”:“value1”,“key2”:“value2”,“key3”:“value3”}

payload.key4 = value4

pm.environment.set("payload",payload)
```

---

<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:** [January 30, 2021, 11:09am UTC](https://community.postman.com/t/add-an-object-to-existing-json/19728/5 "2021-01-30T11:09:07Z")

</div>

@praveendvd Don’t you want to stringify it, otherwise it won’t behave as expected? 🤔

@baljindernohra to add to praveen’s answer…You’d have to update the script like so:

```javascript
let payload = { "key1": "value1", "key2": "value2", "key3": "value3" };

payload.key4 = value4;

// I'd go with local scope variables to avoid polluting the environment
// but you're free to choose global/environment scoped variables also.
pm.variables.set("payload", JSON.stringify(payload));

```

Now in your request body (make sure you **don’t put quotes (")** around the “payload” variable):

```javascript
{
    "branchPayloadDTO": {{payload}}
}

```

---

<div class="post-metadata">

**Author:** ![praveendvd](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/praveendvd/32/6796_2.png) [@praveendvd](https://community.postman.com/u/praveendvd)\
**Post date:** [January 30, 2021, 11:13am UTC](https://community.postman.com/t/add-an-object-to-existing-json/19728/6 "2021-01-30T11:13:58Z")

</div>

you don’t have to stringify it if branchPayloadDTO is an object , Stringify and enclose it with bracket only if its need to be send as a string

`“payload”:"{“key1”:“value1”,“key2”:“value2”,“key3”:“value3”}"`

The payload in question is incorrect as its not escaping the double quotes inside , so can’t say whether the author excepts string or json as payload

---

<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:** [January 30, 2021, 11:21am UTC](https://community.postman.com/t/add-an-object-to-existing-json/19728/7 "2021-01-30T11:21:03Z")

</div>

Nope, the way the data is stored in the environments (or any variable scope) is in a string format.  
So if you don’t stringify it as a JSON before storing it in the env/any variable, then the environment is going to internally call a `.toString` over the object and it’s going to convert it to something that looks like `[object Object]`

See the response from the server which shows what kind of data is received.

**Without stringification:**

 ![Without stringify](https://us1.discourse-cdn.com/getpostman/original/3X/6/0/60e61727c22a7108894872c78a84c41e272dc1c1.png)

**After stringification:**

 ![After stringify](https://us1.discourse-cdn.com/getpostman/original/3X/d/7/d7951fa32311ba0d29f93ce3722136a37dafe6d9.png)

Either way, they would want to send an object and the data and not “[object Object]”

---

<div class="post-metadata">

**Author:** ![praveendvd](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/praveendvd/32/6796_2.png) [@praveendvd](https://community.postman.com/u/praveendvd)\
**Post date:** [January 30, 2021, 11:28am UTC](https://community.postman.com/t/add-an-object-to-existing-json/19728/8 "2021-01-30T11:28:41Z")

</div>

@singhsivcan you are right body except a string 🥳 thanks for the correction

---

<div class="post-metadata">

**Author:** ![baljindernohra](https://avatars.discourse-cdn.com/v4/letter/b/e274bd/32.png) [@baljindernohra](https://community.postman.com/u/baljindernohra)\
**Post date:** [January 30, 2021, 11:31pm UTC](https://community.postman.com/t/add-an-object-to-existing-json/19728/9 "2021-01-30T23:31:24Z")

</div>

@praveendvd @singhsivcan Thanks for the quick response guys. I had to stringify it before setting it as a variable.
