Extract specific value from JSON Response

Hi Team,

I am running one Rest API which gives response as below:

[
    {
        "message": "You can use the OTP 79744 to login to your account. Use it within 3 minutes before it expires. Please do not share this OTP with anyone."
    }
]

So how to extract OTP values in one variable, specifically via regular expression?

However, via JS string function, I can achieve it but wanted to do it via regular expression

let str = `You can use the OTP 79744 to login to your account. Use it within 3 minutes before it expires. Please do not share this OTP with anyone.`;
let substring = str.substring(20,26);
console.log(substring);

I can further utilize the substring variable which contains the OTP values.

Thanks

A very non-robust and hacky way of getting it could be as simple as:

console.log(pm.response.json()[0].message.match(/\d+/)[0])

As it’s not doing a global match, it will get the first number in the message property and then you can select the first item in the match array.

I wouldn’t recommend using that any further that just trying things out locally :grimacing:

Thanks @dannydainton for the prompt response.

1 Like