Search code examples
postmannewmanpostman-pre-request-script

In postman tests, How to assert the response parameter which as special chars in it


I'm using postman tests section to automate the APIs

If the response parameter is something like this "currency": "USD",, then we assert this by pm.expect(jsonResponse.currency, 'USD');

But if the response parameter is something like this "super-card.DCC": "false",, in which key has -, .(dot) in it. Then asserting like this pm.expect(jsonResponse.postingAttributes.super-card.DCC, false); is not working.

Its failing with ReferenceError: card is not defined

Here is the screenshot: enter image description here

Can anybody help me here?


Solution

  • For JSON properties like these, you need to use bracket notation to access the value:

    pm.expect(jsonResponse.postingAttributes["super-card.DCC"], false)
    

    You're not actually asserting anything here though, as long as the property is in the JSON response structure, it will pass the "test", regardless of the value.

    As an example, this endpoint returns the request information and I'm sending a GET request here. Adding the same script as you have, you can see this is passing:

    enter image description here

    The second argument in the pm.expect() function is an optional message that would be shown if the test failed.

    enter image description here

    You would need to use:

    pm.expect(jsonResponse.postingAttributes["super-card.DCC"]).to.equal("false")