# How to get from nested objects

**URL:** <https://community.postman.com/t/how-to-get-from-nested-objects/7769>\
**Category:** Help Hub\
**Tags:** tests, collections\
**Created:** [September 13, 2019, 6:47am UTC](https://community.postman.com/t/how-to-get-from-nested-objects/7769 "2019-09-13T06:47:14Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![ash1234](https://avatars.discourse-cdn.com/v4/letter/a/898d66/32.png) [@ash1234](https://community.postman.com/u/ash1234)\
**Post date:** [September 13, 2019, 6:47am UTC](https://community.postman.com/t/how-to-get-from-nested-objects/7769/1 "2019-09-13T06:47:15Z")

</div>

How to retrieve ABCD1234 from the below nested object. I want to set this as env.variable.  
How can this be done?

{  
“Name”: “TEST1”,  
“FileName”: “TEST2.txt”,  
“ItemIds”: {  
“ABCD1234”: {  
…}  
}  
}

---

<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:** [September 13, 2019, 7:19am UTC](https://community.postman.com/t/how-to-get-from-nested-objects/7769/2 "2019-09-13T07:19:54Z")

</div>

Hello @ash1234, welcome to the community! 👋

Is this your response body?

If yes, you’ll need to write something like this in the **Test script** :

```javascript
let response = pm.response.json(),
   data = response["ItemIds"]["ABCD1234"];

pm.environment.set("abcd1234", JSON.stringify(data));

```

And to read this value in the test scripts, you’ll need to write:

```javascript
let abcdData = JSON.parse(pm.environment.get("abcd1234"));

```

---

<div class="post-metadata">

**Author:** ![ash1234](https://avatars.discourse-cdn.com/v4/letter/a/898d66/32.png) [@ash1234](https://community.postman.com/u/ash1234)\
**Post date:** [September 13, 2019, 7:22am UTC](https://community.postman.com/t/how-to-get-from-nested-objects/7769/3 "2019-09-13T07:22:21Z")

</div>

Thank you , here ABCD1234 gets generated dynamically.

---

<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:** [September 13, 2019, 7:23am UTC](https://community.postman.com/t/how-to-get-from-nested-objects/7769/4 "2019-09-13T07:23:51Z")

</div>

Oh, so basically whatever is there in the `ItemIds`, you need to set that as an environment variable.

That’s what you want to achieve?  
Also, does `ItemIds` contain multiple properties?

For eg:

```javascript
{
  "Name": "TEST1",
  "FileName": "TEST2.txt",
  "ItemIds": {
    "ABCD1234": {…},
    "ABCD83929": {…},
    "XYZ1234": {…}
  }
}

```

What would you want out of this then?  
Should all the values be stored as environment variables?

---

<div class="post-metadata">

**Author:** ![ash1234](https://avatars.discourse-cdn.com/v4/letter/a/898d66/32.png) [@ash1234](https://community.postman.com/u/ash1234)\
**Post date:** [September 13, 2019, 7:27am UTC](https://community.postman.com/t/how-to-get-from-nested-objects/7769/5 "2019-09-13T07:27:29Z")

</div>

Its a nested one, and itemIds have only 1 value. i want to retrived only ItemsIds first value.

{  
“Name”: “TEST1”,  
“FileName”: “TEST2.txt”,  
“ItemIds”: {  
“ABCD1234”: {  
“ABCD83929”: {  
…},  
}  
}

---

<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:** [September 13, 2019, 7:29am UTC](https://community.postman.com/t/how-to-get-from-nested-objects/7769/6 "2019-09-13T07:29:39Z")

</div>

So what you could do is something like this:

```javascript
let response = pm.response.json(),
   itemKey = Object.keys(response["ItemIds"])[0];

// This itemKey will now be dynamic based on the data
pm.environment.set("itemKey", itemKey);

```

---

<div class="post-metadata">

**Author:** ![ash1234](https://avatars.discourse-cdn.com/v4/letter/a/898d66/32.png) [@ash1234](https://community.postman.com/u/ash1234)\
**Post date:** [September 13, 2019, 7:46am UTC](https://community.postman.com/t/how-to-get-from-nested-objects/7769/7 "2019-09-13T07:46:35Z")

</div>

This worked thank you 🙏🙏

let response = pm.response.json(),  
itemKey = Object.keys(response[“ItemIds”])[0],  
itemIdData = response[“ItemIds”][itemKey];

//why are getting itemIdData

pm.environment.set(“Item\_key”, itemKey);

---

<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:** [September 13, 2019, 7:48am UTC](https://community.postman.com/t/how-to-get-from-nested-objects/7769/8 "2019-09-13T07:48:30Z")

</div>

Awesome!  
I thought you needed the itemData too. 🙂

Anyway, it’s up-to you what you’d like to do with the script, the goal of our community is to point you in the right direction which I think we’ve achieved here!

---

<div class="post-metadata">

**Author:** ![NitiJabin](https://avatars.discourse-cdn.com/v4/letter/n/e99b99/32.png) [@NitiJabin](https://community.postman.com/u/NitiJabin)\
**Post date:** [September 13, 2019, 8:18pm UTC](https://community.postman.com/t/how-to-get-from-nested-objects/7769/9 "2019-09-13T20:18:18Z")

</div>

Hi, I am getting a similar problem. I tried the way you have mentioned but not working for me. I want to validate the message inside the errors.

let res = pm.response.json(),  
itemKey = Object.keys(res[“errors”])[1],  
itemIdData = res[“errors”][itemKey];

console.log("Message new:: "+ itemIdData);

```
pm.test('Verify message for Login with empty password ', ()=>{
    pm.expect(itemIdData).is.to.equal("should NOT be shorter than 8 characters");
});

```

 ![image](https://us1.discourse-cdn.com/getpostman/original/3X/3/2/3243a172bbf0765f83071ec2c89c3d73fda0d8c2.png)

---

<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:** [September 14, 2019, 5:17am UTC](https://community.postman.com/t/how-to-get-from-nested-objects/7769/10 "2019-09-14T05:17:23Z")

</div>

@NitiJabin, as I can see that all you already know that if the error is there, then you just need to get the message.

You don’t need to do the dynamic key thing, also because your `errors` object has multiple properties and when you do `Object.keys`, the order of the keys of an object is not guaranteed in the array, it could change.

What you can actually write would be this:

```javascript
let response = pm.response.json();

pm.test('Verify message for Login with empty password', () => {
  let errorMessage = _.get(response, 'errors.message'),
    expectedErrorMessage = 'should NOT be shorter than 8 characters';

  pm.expect(errorMessage).to.equal(expectedErrorMessage);
});

```

This keeps your code readable and concise.

---

<div class="post-metadata">

**Author:** ![NitiJabin](https://avatars.discourse-cdn.com/v4/letter/n/e99b99/32.png) [@NitiJabin](https://community.postman.com/u/NitiJabin)\
**Post date:** [September 22, 2019, 6:27am UTC](https://community.postman.com/t/how-to-get-from-nested-objects/7769/11 "2019-09-22T06:27:48Z")

</div>

Thank you. It is working.

---

<div class="post-metadata">

**Author:** ![austin4778](https://avatars.discourse-cdn.com/v4/letter/a/73ab20/32.png) [@austin4778](https://community.postman.com/u/austin4778)\
**Post date:** [March 5, 2020, 7:00pm UTC](https://community.postman.com/t/how-to-get-from-nested-objects/7769/12 "2020-03-05T19:00:29Z")

</div>

I have a similar use case as this. I have a API call that returns the below. Would it be possible to take the responses out of this call, set them as env variables and then use those variables in another API call to post the data I require? I am looking to obtain the SubscriptionQty, ProductId, and AccountId then take those values and utilize them in this second API call to post data.

[  
{  
“SubscriptionQty”: “1.0”,  
“ProductName”: “Product A”,  
“ProductId”: “12345”,  
“AccountName”: “Customer A”,  
“AccountId”: “66723”,  
“ProductID”: “7JQ-00341”,  
“Message”: “successful”

},  
{  
“SubscriptionQty”: “10.0”,  
“ProductName”: “Product A”,  
“ProductId”: “12345”,  
“AccountName”: “Customer B”,  
“AccountId”: “66746”,  
“ProductID”: “7JQ-00341”,  
“Message”: “successful”

---

<div class="post-metadata">

**Author:** ![austin4778](https://avatars.discourse-cdn.com/v4/letter/a/73ab20/32.png) [@austin4778](https://community.postman.com/u/austin4778)\
**Post date:** [March 9, 2020, 8:25pm UTC](https://community.postman.com/t/how-to-get-from-nested-objects/7769/13 "2020-03-09T20:25:14Z")

</div>

Figured this out by using [https://thisendout.com/2017/02/22/loops-dynamic-variables-postman-pt2/](https://thisendout.com/2017/02/22/loops-dynamic-variables-postman-pt2/)

---

<div class="post-metadata">

**Author:** ![poornapragnaps](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/poornapragnaps/32/7529_2.png) [@poornapragnaps](https://community.postman.com/u/poornapragnaps)\
**Post date:** [March 3, 2021, 9:08pm UTC](https://community.postman.com/t/how-to-get-from-nested-objects/7769/14 "2021-03-03T21:08:16Z")

</div>

Hi There, similar issue, with json data – need to verify one the element (i.e. “sipaddress”: “+a682@com.com”) element name sipaddress has more than 200+, so how verify this key and value ? Please help me.  
{

```
"message": "All SIP addresses fetched successfully.",

"data": [

    {

        "tenant": "Pit",

        "location": "LOC761",

        "provider": "TTEC22",

        "sipaddress": "23632224022021",

        "inUse": false,

        "updatedAt": 1614200241078

    },

    {

        "tenant": "Pit",

        "location": "LOC771",

        "sipaddress": "+5734353@qa22.org",

        "updatedAt": 1614200222359,

        "createdAt": 1614118371824,

        "provider": "TTEC22",

        "objectGUID": "3b96d6e5-681d-44cb-9144-6fc5b0702300",

        "inUse": false

    },

    {

        "tenant": "Pit",

        "location": "MA",

        "sipaddress": "+7434452@qa13.org",

        "updatedAt": 1614118369411,

        "createdAt": 1614118369411,

        "provider": "TTEC13",

        "objectGUID": "62046239-c6aa-453a-8953-d500ce270e3c",

        "inUse": false

    },

    {

        "tenant": "Pit",

        "location": "MA",

        "sipaddress": "+6221418@qa32.org",

        "updatedAt": 1614118374484,

        "createdAt": 1614118374484,

        "provider": "TTEC32",

        "objectGUID": "88ac5f0a-926d-47a5-a7c6-d4d0d2612e96",

        "inUse": false

    },

    {

        "tenant": "Axecap",

        "location": "IN",

        "sipaddress": "+9053@six.com",

        "updatedAt": 1614697684203,

        "createdAt": 1614697684203,

        "provider": "AWS",

        "objectGUID": "253cb226-6fa1-47bc-8010-143f1d11e6ec",

        "inUse": true

    },

    {

        "tenant": "Pit",

        "location": "LOC814",

        "sipaddress": "63634524022021",

        "updatedAt": 1614200473775,

        "createdAt": 1614200473775,

        "provider": "TTEC22",

        "objectGUID": "5e357fdf-449f-4515-b943-45979818cbf1",

        "inUse": false

    },

    {

        "tenant": "Axecap",

        "location": "US",

        "sipaddress": "+1111@abc.com",

        "updatedAt": 1614693991996,

        "createdAt": 1614286862404,

        "provider": "AWS",

        "objectGUID": "cf9968cc-31f0-470e-9c35-48218f7cac8e",

        "inUse": false

    },

    {

        "tenant": "Pit",

        "location": "MA",

        "sipaddress": "+4974113@qa26.org",

        "updatedAt": 1614118372951,

        "createdAt": 1614118372951,

        "provider": "TTEC26",

        "objectGUID": "60641345-285a-4fe5-9acd-5b9a6031fb00",

        "inUse": false

    },

    {

        "tenant": "Axecap",

        "location": "IN",

        "sipaddress": "+y559@org.com",

        "updatedAt": 1614711139290,

        "createdAt": 1614711139290,

        "provider": "AWS",

        "objectGUID": "fe08d4a0-9777-46b4-b964-ca50f18ba085",

        "inUse": true

    },

    {

        "tenant": "Axecap",

        "location": "IN",

        "sipaddress": "+h98@net",

        "updatedAt": 1614710904665,

        "createdAt": 1614710904665,

        "provider": "AWS",

        "objectGUID": "b4633ea6-4ab5-467a-8c90-60385f1910eb",

        "inUse": true

    },

    {

        "tenant": "Pit",

        "location": "LOC667",

        "sipaddress": "15662324022021",

        "updatedAt": 1614200496183,

        "createdAt": 1614200496183,

        "provider": "TTEC22",

        "objectGUID": "07ad2444-6b5d-4934-ae95-2829732838b4",

        "inUse": false

    },

    {

        "tenant": "Pit",

        "location": "MA",

        "sipaddress": "+3639236@qa1.org",

        "updatedAt": 1614118366209,

        "createdAt": 1614118366209,

        "provider": "TTEC1",

        "objectGUID": "0293bb06-8615-4fa1-83f4-9a7182c8df4f",

        "inUse": false

    },

    {

        "tenant": "Pit",

        "location": "MA",

        "sipaddress": "+4774338@qa10.org",

        "updatedAt": 1614118368563,

        "createdAt": 1614118368563,

        "provider": "TTEC10",

        "objectGUID": "42e03ef9-bfb7-4268-8222-222e125bf81e",

        "inUse": false

    },

    {

        "tenant": "Axecap",

        "location": "US",

        "sipaddress": "+bbc@abc.co",

        "updatedAt": 1614240405075,

        "createdAt": 1614240405075,

        "provider": "AWS",

        "objectGUID": "a0c7b89f-9f87-4386-9fab-1239a4d90df5",

        "inUse": false

    },

    {

        "tenant": "Axecap",

        "location": "IN",

        "sipaddress": "+582o@six.com",

        "updatedAt": 1614697627524,

        "createdAt": 1614697627524,

        "provider": "AWS",

        "objectGUID": "3e2b00b7-10ed-4ff6-8381-053ac8106fb2",

        "inUse": true

    },

    {

        "tenant": "Pit",

        "location": "MA",

        "sipaddress": "6762@edit.org",

        "updatedAt": 1614193088758,

        "createdAt": 1614193088758,

        "provider": "TTEC22",

        "objectGUID": "56334323-d0d6-4cb2-960a-cf2ffff5541d",

        "inUse": false

    },

    {

        "tenant": "Axecap",

        "location": "IN",

        "sipaddress": "+9448@six.com",

        "updatedAt": 1614698848550,

        "createdAt": 1614698848550,

        "provider": "AWS",

        "objectGUID": "5ad33c98-6fb0-4b07-a068-eecfe1b75d5f",

        "inUse": true

    },

    {

        "tenant": "Pit",

        "location": "MA",

        "sipaddress": "+8969289@qa34.org",

        "updatedAt": 1614118375044,

        "createdAt": 1614118375044,

        "provider": "TTEC34",

        "objectGUID": "61ba286c-751a-4081-a34b-f9abf6ed515d",

        "inUse": false

    },

    {

        "tenant": "Pit",

        "location": "LOC794",

        "sipaddress": "64151224022021",

        "updatedAt": 1614200562530,

        "createdAt": 1614200562530,

        "provider": "TTEC22",

        "objectGUID": "35def9e4-742f-4fbf-ab18-0a6ad2bb84f7",

        "inUse": false

    },

    {

        "tenant": "Axecap",

        "location": "IN",

        "sipaddress": "+689o@six.com",

        "updatedAt": 1614698685265,

        "createdAt": 1614698685265,

        "provider": "AWS",

        "objectGUID": "15f4641b-1cb3-46e5-a252-b62eb7571553",

        "inUse": true

    },

    {

        "tenant": "Pit",

        "location": "LOC512",

        "sipaddress": "45346424022021",

        "updatedAt": 1614200620612,

        "createdAt": 1614200620612,

        "provider": "TTEC22",

        "objectGUID": "77f1c360-5068-4592-b53d-dcac34ccca03",

        "inUse": false

    },

    {

        "tenant": "Pit",

        "location": "MA",

        "sipaddress": "+5851197@qa59.org",

        "updatedAt": 1614118381970,

        "createdAt": 1614118381970,

        "provider": "TTEC59",

        "objectGUID": "fde59156-b5b5-4641-ba96-9bcf4d5a12f0",

        "inUse": false

    },

    {

        "tenant": "Axecap",

        "location": "UAE",

        "sipaddress": "+abc123@abc.com",

        "updatedAt": 1614785188591,

        "createdAt": 1614785188591,

        "provider": "Google",

        "objectGUID": "ac9e4cdf-b09e-4411-a780-48004746fe45",

        "inUse": null

    },

    {

        "tenant": "Pit",

        "location": "SG",

        "sipaddress": "+abc123@abc.com",

        "updatedAt": 1614784658699,

        "createdAt": 1614784658699,

        "provider": "IBM Cloud",

        "objectGUID": "7eedba1f-3362-4794-b3e8-fccf4e26a6df",

        "inUse": true

    },

    {

        "tenant": "Pitburg",

        "location": "UK",

        "sipaddress": "+abc123@abc.com",

        "updatedAt": 1614785220664,

        "createdAt": 1614785220664,

        "provider": "AWS",

        "objectGUID": "dbce4ecd-63a6-415d-a2f9-c3b0dc7d5708",

        "inUse": true

    },

    {

        "tenant": "Pit",

        "location": "SG",

        "sipaddress": "+26@ttec.org",

        "updatedAt": 1614359675313,

        "createdAt": 1614359675313,

        "provider": "IBM Cloud",

        "objectGUID": "79d114d0-fd2d-4668-97a2-8ed4c0fb2f99",

        "inUse": true

    },

    {

        "tenant": "Pit",

        "location": "MA",

        "sipaddress": "+8729927@qa60.org",

        "updatedAt": 1614118382188,

        "createdAt": 1614118382188,

        "provider": "TTEC60",

        "objectGUID": "d713ceeb-8967-4832-928a-a64440381c5d",

        "inUse": false

    },

    {

        "tenant": "Axecap",

        "location": "IN",

        "sipaddress": "+2325@net.com",

        "updatedAt": 1614711878380,

        "createdAt": 1614711878380,

        "provider": "AWS",

        "objectGUID": "51b2e02f-b46b-41c4-9107-eff417f1cf0e",

        "inUse": true

    },

    {

        "tenant": "Pit",

        "location": "MA",

        "sipaddress": "address0@qa.org",

        "updatedAt": 1614096011448,

        "createdAt": 1614096008786,

        "provider": "TTEC0",

        "objectGUID": "18791f75-7ccb-43cd-b0fd-8b0b370018bd",

        "inUse": true

    },

    {

        "tenant": "Pit",

        "location": "MA",

        "sipaddress": "+5247338@qa57.org",

        "updatedAt": 1614118381445,

        "createdAt": 1614118381445,

        "provider": "TTEC57",

        "objectGUID": "d0253a7d-0562-471d-80b6-51e3ad2a245e",

        "inUse": false

    },

    {

        "tenant": "Pit",

        "location": "MA",

        "sipaddress": "+99197539@qa0.org",

        "updatedAt": 1614118167529,

        "createdAt": 1614118167529,

        "provider": "TTEC0",

        "objectGUID": "626b0e85-195e-47d6-94e2-69d3e9fcc326",

        "inUse": false
    },

```

{

```
        "tenant": "Axecap",

        "location": "IN",

        "sipaddress": "+6559@com.com",

        "updatedAt": 1614714147549,

        "createdAt": 1614714147549,

        "provider": "AWS",

        "objectGUID": "b58d23ee-6972-4c62-9c6f-66ddc8db8f85",

        "inUse": true

    }

```

],

```
"total": 243
```

---

<div class="post-metadata">

**Author:** ![Psingh04](https://avatars.discourse-cdn.com/v4/letter/p/ac8455/32.png) [@Psingh04](https://community.postman.com/u/Psingh04)\
**Post date:** [July 30, 2021, 12:27pm UTC](https://community.postman.com/t/how-to-get-from-nested-objects/7769/15 "2021-07-30T12:27:26Z")

</div>

> [@poornapragnaps](#):
>
> `23632224022021`

Do you get the answer? I am facing same issue

---

<div class="post-metadata">

**Author:** ![spacecraft-astronom6](https://avatars.discourse-cdn.com/v4/letter/s/65b543/32.png) [@spacecraft-astronom6](https://community.postman.com/u/spacecraft-astronom6)\
**Post date:** [December 28, 2022, 4:28pm UTC](https://community.postman.com/t/how-to-get-from-nested-objects/7769/16 "2022-12-28T16:28:05Z")

</div>

I am also facing same issue…Plz ping the solution.

Thanks in advance.

---

<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 1, 2023, 4:55pm UTC](https://community.postman.com/t/how-to-get-from-nested-objects/7769/17 "2023-01-01T16:55:33Z")

</div>

@spacecraft-astronom6

I would recommend posing a new question and include a sample response.

This can all be done using the JavaScript find function.

Using the example posted above.

```auto
let response = pm.response.json();
let search = (response.data.find(obj => {return obj.sipaddress === 'a682@com.com'}));
console.log(search);

```

The JavaScript find function only works against arrays. The data element in this example is an array (hence you need to post an example response to ensure its the same and that it is actually an array).

Your test is as simple as checking that search is not undefined.
