# How to verify dynamically changing attribute names in json response

**URL:** <https://community.postman.com/t/how-to-verify-dynamically-changing-attribute-names-in-json-response/18171>\
**Category:** Help Hub\
**Tags:** variables\
**Created:** [December 2, 2020, 3:02pm UTC](https://community.postman.com/t/how-to-verify-dynamically-changing-attribute-names-in-json-response/18171 "2020-12-02T15:02:59Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ankur\_Jindal](https://avatars.discourse-cdn.com/v4/letter/a/5e9695/32.png) [@Ankur\_Jindal](https://community.postman.com/u/Ankur_Jindal)\
**Post date:** [December 2, 2020, 3:02pm UTC](https://community.postman.com/t/how-to-verify-dynamically-changing-attribute-names-in-json-response/18171/1 "2020-12-02T15:02:59Z")

</div>

Hi,

I got stuck in verifying the attribute names which are not consistent always.  
E.g. The attribute name in response is =\>

“ **email\_families\_opt\_indicator** ”: “true”

Now next time the attribute name might come as =\>  
“ **email\_individual\_opt\_indicator** ”: “true”

I wanted to parametrize above attribute name so that or is picked up from a variable and then accordingly the attribute name is verified.

Below is the code with hardcoded value for attribute name =\>

pm.test(“Opt Indicator - Expected true”, function () {  
pm.expect(“true”).to.eql(response.traits.email\_ **families** \_opt\_indicator);  
});

Any help on above would be appreciated. Thanks.

---

<div class="post-metadata">

**Author:** ![prabhumohan09](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/prabhumohan09/32/5961_2.png) [@prabhumohan09](https://community.postman.com/u/prabhumohan09)\
**Post date:** [December 2, 2020, 6:25pm UTC](https://community.postman.com/t/how-to-verify-dynamically-changing-attribute-names-in-json-response/18171/2 "2020-12-02T18:25:24Z")

</div>

You can try with OR operator to validate the flag value. or else use if condition to check the corresponding flag then do assertion. @Ankur_Jindal

---

<div class="post-metadata">

**Author:** ![allenheltondev](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/allenheltondev/32/31738_2.png) [@allenheltondev](https://community.postman.com/u/allenheltondev)\
**Post date:** [December 3, 2020, 12:35pm UTC](https://community.postman.com/t/how-to-verify-dynamically-changing-attribute-names-in-json-response/18171/3 "2020-12-03T12:35:06Z")

</div>

If you set the name of the property to a variable you can access it dynamically through square brackets. You could also check for existence then do an assertion.

Example 1

```auto
let property = 'email_families_opt_indicator';
if(someCondition){
  property = 'email_individiual_opt_indicator';
}

pm.test('Opt Indicator - Expected true', function(){
  pm.expect(response.traits[property]).to.be.true;
}

```

Example 2

```auto
pm.test('Opt Indicator - Expected true', function(){
  if(response.traits.email_families_opt_indicator){
    pm.expect(response.traits.email_families_opt_indicator).to.be.true;
  } else {
    pm.expect(response.traits.email_individual_opt_indicator).to.be.true;
  }
}

```

---

<div class="post-metadata">

**Author:** ![Ankur\_Jindal](https://avatars.discourse-cdn.com/v4/letter/a/5e9695/32.png) [@Ankur\_Jindal](https://community.postman.com/u/Ankur_Jindal)\
**Post date:** [December 18, 2020, 1:04pm UTC](https://community.postman.com/t/how-to-verify-dynamically-changing-attribute-names-in-json-response/18171/4 "2020-12-18T13:04:34Z")

</div>

Thanks Allen. I have tried Example 1 as Example 2 is not feasible in this specific scenario due to some other constraints.

This is working for me. Very much appreciate your help!! I couldn’t find this anywhere how to parametrize the response attribute names. From where you came to know of this? Any good website read would you recommend?

Thanks Again.

---

<div class="post-metadata">

**Author:** ![allenheltondev](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/allenheltondev/32/31738_2.png) [@allenheltondev](https://community.postman.com/u/allenheltondev)\
**Post date:** [December 18, 2020, 1:20pm UTC](https://community.postman.com/t/how-to-verify-dynamically-changing-attribute-names-in-json-response/18171/5 "2020-12-18T13:20:38Z")

</div>

Happy to help!

I know how to do that just from my programming background. I can’t really point you at any resources that would tell you things like that.

Best thing I can say to do is just keep practicing. Things will make more and more sense over time.

---

<div class="post-metadata">

**Author:** ![Ankur\_Jindal](https://avatars.discourse-cdn.com/v4/letter/a/5e9695/32.png) [@Ankur\_Jindal](https://community.postman.com/u/Ankur_Jindal)\
**Post date:** [December 18, 2020, 1:21pm UTC](https://community.postman.com/t/how-to-verify-dynamically-changing-attribute-names-in-json-response/18171/6 "2020-12-18T13:21:36Z")

</div>

Exactly 🙂

---

<div class="post-metadata">

**Author:** ![praveendvd](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/praveendvd/32/6796_2.png) [@praveendvd](https://community.postman.com/u/praveendvd)\
**Post date:** [December 20, 2020, 3:15pm UTC](https://community.postman.com/t/how-to-verify-dynamically-changing-attribute-names-in-json-response/18171/7 "2020-12-20T15:15:22Z")

</div>

```
pm.test(“Opt Indicator - Expected true”, function () {
pm.expect(“true”).to.eql(response.traits[`email_ ${pm.environment.get('variable')} _opt_indicator`]);
});

```

you can use pm.environment.get(‘varaiblename’) , to get the value and call it as json[propertystring]

To get it dynamically

```
array=Object.keys(response.traits)

c=array[array.findIndex((element) => element.includes("opt"))]

    pm.test(“Opt Indicator - Expected true”, function () {
    pm.expect(“true”).to.eql(response.traits[c]);
    });
```

---

<div class="post-metadata">

**Author:** ![Ankur\_Jindal](https://avatars.discourse-cdn.com/v4/letter/a/5e9695/32.png) [@Ankur\_Jindal](https://community.postman.com/u/Ankur_Jindal)\
**Post date:** [December 23, 2020, 9:33am UTC](https://community.postman.com/t/how-to-verify-dynamically-changing-attribute-names-in-json-response/18171/8 "2020-12-23T09:33:54Z")

</div>

Thanks, this is also a good idea!!
