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

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

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


This keeps your code readable and concise.

Thank you. It is working.