Extract string from response

My response looks like this

{
    "code": 201,
    "message": "192.168.1.106/repo/api/v1/report/23",
    "type": "INFO"
}

But i only need the 23 as environment variable.
My test script looks like this what do i have to do to only get the 23?
pm.environment.set(‘DownloadLink’, pm.response.json().message);

You could take a look at the .split() JS method to get that part of the string. This will create a new array of values based on the separator provided. Then you would need to specify the item you want, this will be zero-indexed so 0 would be the first item.

For example:

let messageString = pm.response.json().message

pm.environment.set("DownloadLink", messageString.split('/')[5])

This obviously fails to extract that value is the position were to change.

Lots of different examples can be found here:

2 Likes

How about “report”, I only need the report as environment variable.

Hello @cryosat-physicist-41, yes its simple

in the above example,

let messageString = pm.response.json().message

pm.environment.set("DownloadLink", messageString.split('/')[4])

This returns the value “report”.

Please try to go through the link which Danny shared. All the substrings are separated and stored in an array :blush:

This script returns the following error for me:

“There was an error in evaluating the test script: TypeError: Cannot read property ‘split’ of undefined”.

I need to capture only the “customer123@example.com” value as a variable from the “description” string below. Is the error occurring because “description” is nested in the ticket property? Can you suggest adjustments to script to capture the value as a variable?

{
“ticket”: {
“id”: 8218406,
“external_id”: null,
“created_at”: “2021-10-08T16:59:57Z”,
“updated_at”: “2021-10-08T18:07:24Z”,
“type”: null,
“subject”: “[EXTERNAL] Customer Name”,
“raw_subject”: “[EXTERNAL] Customer Name”,
“description”: “Email sent on behalf of customer123@example.com”,
“priority”: “normal”,
“status”: “new”
}
}

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