Retrieve string value in array from an response

Below is the response for my request

{
  "code": 401,
  "reason": "Unauthorized",
  "detail": {
    "failureUrl": "{\"code\":403,\"reason\":\"Forbidden\",\"message\":\"Policy validation failed\",\"detail\":{\"result\":false,\"failedPolicyRequirements\":[{\"policyRequirements\":[{\"policyRequirement\":\"VALID_NAME_FORMAT\"}],\"property\":\"givenName\"}]}}"
  }
}

I need to verify {\"policyRequirement\":\"VALID_NAME_FORMAT\"} policyRequirement value. I am not able to achieve it array.

Can you please help

The failureUrl key is a stringified object, so you will need to parse it before you can access the elements within.

The value for failedPolicyRequirements is an array with a single object in it and so is the policyRequirements key that sits just underneath.

The following should target the policyRequirement key.

const response = pm.response.json()

let failureUrl = JSON.parse(response.detail.failureUrl);

let policyRequirement = failureUrl.detail.failedPolicyRequirements[0].policyRequirements[0].policyRequirement

pm.test("policyRequirement = VALID_NAME_FORMAT", () => {
    pm.expect(policyRequirement).to.eql("VALID_NAME_FORMAT");
});
1 Like

Thank you @michaelderekjones It works

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.