# How to add nested object to existing json

**URL:** <https://community.postman.com/t/how-to-add-nested-object-to-existing-json/42629>\
**Category:** Help Hub\
**Created:** [January 5, 2023, 8:07pm UTC](https://community.postman.com/t/how-to-add-nested-object-to-existing-json/42629 "2023-01-05T20:07:05Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![michaelderekjones](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/michaelderekjones/32/14819_2.png) [@michaelderekjones](https://community.postman.com/u/michaelderekjones)\
**Post date:** [January 6, 2023, 12:33pm UTC](https://community.postman.com/t/how-to-add-nested-object-to-existing-json/42629/2 "2023-01-06T12:33:37Z")

</div>

You have to stringify the responseData before saving it to the “body” environment variable. (Like you do for the updatedBody).

Then parse it when you retrieve it.

You can parse this in one line.

```auto
let variableName = JSON.parse(pm.environment.get("body"));

```

If you use let or var instead of const, then you can update the elements directly.  
You don’t really need to use push.

PS: This is a JavaScript object at this point.

In your example, the product.push is incorrect, as surely it should be jsonObject.info.product.

Not sure if its will a valid object after this. (Hence the error).

You can also just define the whole product element, and update it in one go. Like following.

```auto
let response = pm.response.json().data; // parse JSON response into JavaScript object.

console.log(response); // original response.

// define product element to replace null entries
const productNull = {
    "productId": 123,
    "info": {
        "available": true,
        "restock": {
        "enabled": true
        }
    }
}

if (response.info.product ==null) {
    response.info.product = productNull // update response
}

console.log(response) // updated response - remember to stringify before saving to environment\collection variable.

```

![image](https://us1.discourse-cdn.com/getpostman/original/3X/8/c/8cc5e0b713bba0e03e0e4860e7d54f26b1968a43.png)

---

_[View the full topic](https://community.postman.com/t/how-to-add-nested-object-to-existing-json/42629)._
