How can I validate a number that is treated as text in JSON response, in my Test Script assertion?

Hello, new to Postman, newer still to JavaScript

My question:
In a test script I want to validate a response is within a given range, but the problem is the number format is Brazilian, (1,1 instead of 1.1) and is so surrounded by β€œβ€ and treated like text.

My Code Attempt:
pm.test(β€œTest_07 AI % is in range 3,5 to 4,5”, function () {
var annualMode = pm.response.json();
pm.expect(annualMode.recScheme.incPctAnMode).is.lessThan(β€œ5,0”);

Obviously this fails. AssertionError: expected β€˜3,7’ to be a number or a date

I’d be very grateful for any help.

Hi @jes73,

First you will need to change the string from 1,1 to 1.1. You can do this with simple javascript string replace text.replace(",", β€œ.”).

Then you will need to convert the text to an integer again with simple javascript. parseInt(text).

So your test should look like this…

pm.test(β€œTest_07 AI % is in range 3,5 to 4,5”, function () {
var annualMode = pm.response.json();
var value = annualMode.recScheme.incPctAnMode.replace(’,’,’.’) // Setting new value and replacing the character.
pm.expect(parseInt(value)).is.lessThan(5.0);

2 Likes

Oh my! Thank you so much @archerZ - you made that look very simple! :smiley:
That has helped enormously.