# 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:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![sofia365](https://avatars.discourse-cdn.com/v4/letter/s/c2a13f/32.png) [@sofia365](https://community.postman.com/u/sofia365)\
**Post date:** [January 5, 2023, 8:07pm UTC](https://community.postman.com/t/how-to-add-nested-object-to-existing-json/42629/1 "2023-01-05T20:07:05Z")

</div>

I need to modify json file and not sure how to do it.

First, I send GET request that returns JSON:

```auto
{
"id": 100,
"info": {
      "type": "abc",
      "product": null
         }
}

```

…and I save it into var:

```auto
var responseData = pm.response.json();
pm.environment.set("body", responseData);

```

Next, I need to send PUT request with updated json file. If product=null it will look like :

```auto
{
"id": 100,
"info":
    "type": "abc",
    "product": {
          "productId": 123,
           "info": {
                 "available": true,
                  "restock": {
                     "enabled": true
                  }
             }
}
}

```

In my pre-request script I have the following:

```auto
var requestBody = pm.environment.get("body");

if (requestBody.info.product !==null) {

requestBody.info.product =null

} else {

jsonObject = JSON.parse(requestBody);

jsonObject.product.push({"productId":"123"});

// I am not sure how to add the rest...

pm.environment.set("updatedBody", JSON.stringify(requestBody));

```

When I run it I am getting the error: **JSONError: Unexpected token ‘o’ at 1:2 [object Object] ^**

---

<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)

---

<div class="post-metadata">

**Author:** ![perezben](https://avatars.discourse-cdn.com/v4/letter/p/e480ec/32.png) [@perezben](https://community.postman.com/u/perezben)\
**Post date:** [January 9, 2023, 5:37pm UTC](https://community.postman.com/t/how-to-add-nested-object-to-existing-json/42629/3 "2023-01-09T17:37:16Z")

</div>

Thank you @michaelderekjones , I’ll check this

have a nice day
