How to get from nested objects

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”: {
…}
}
}

Hello @ash1234, welcome to the community! :wave:

Is this your response body?

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

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:

let abcdData = JSON.parse(pm.environment.get("abcd1234"));
1 Like

Thank you , here ABCD1234 gets generated dynamically.

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:

{
  "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?

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”: {
…},
}
}

So what you could do is something like this:

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);
1 Like

This worked thank you :pray::pray:

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

Awesome!
I thought you needed the itemData too. :slight_smile:

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!

1 Like

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");
});

@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:

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.

Thank you. It is working.

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”

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

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

Do you get the answer? I am facing same issue

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

Thanks in advance.

@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.

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.