How to assert JSON response body

Hi All,

I am try to test one of my API using POSTMAN tests. Below is my response JOSON

{
    "privileges[3].privilegeName": "Privilege name is mandatory",
    "privileges[2].privilegeName": "Privilege name should be Alphanumeric with underscore (_) and/or hyphen (-)",
    "privileges[0].privilegeName": "Privilege name should be Alphanumeric with underscore (_) and/or hyphen (-)",
    "privileges[1].privilegeName": "Privilege name should be at least 5 characters and not more than 80 characters"
}

Now, my problem is how to get the value of JSON property(field) - “privileges[3].privilegeName” as that I can assert it.

Thanks in advance

You can use bracket notation (which puts the key in a string).

Useful when the key has special characters like in your case.

const response = pm.response.json();
console.log(response["privileges[3].privilegeName"]);
3 Likes

It worked!! fantastic!!
Thank you Mike!. I sure appreciate your speedy help :slight_smile:

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