How to verify dynamically changing attribute names in json response


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.

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

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

let property = 'email_families_opt_indicator';
  property = 'email_individiual_opt_indicator';

pm.test('Opt Indicator - Expected true', function(){

Example 2

pm.test('Opt Indicator - Expected true', function(){
  } else {

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.

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.

Exactly :slightly_smiling_face:

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


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

    pm.test(“Opt Indicator - Expected true”, function () {
1 Like

Thanks, this is also a good idea!!