# Extract string from response

**URL:** <https://community.postman.com/t/extract-string-from-response/7473>\
**Category:** Help Hub\
**Tags:** variables, environments\
**Created:** [August 28, 2019, 7:49am UTC](https://community.postman.com/t/extract-string-from-response/7473 "2019-08-28T07:49:49Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Lukas.lobnig](https://avatars.discourse-cdn.com/v4/letter/l/d26b3c/32.png) [@Lukas.lobnig](https://community.postman.com/u/Lukas.lobnig)\
**Post date:** [August 28, 2019, 7:49am UTC](https://community.postman.com/t/extract-string-from-response/7473/1 "2019-08-28T07:49:49Z")

</div>

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);

---

<div class="post-metadata">

**Author:** ![danny-dainton](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/danny-dainton/32/29401_2.png) [@danny-dainton](https://community.postman.com/u/danny-dainton)\
**Post date:** [August 28, 2019, 8:00am UTC](https://community.postman.com/t/extract-string-from-response/7473/2 "2019-08-28T08:00:46Z")

</div>

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:

> **[String.prototype.split() - JavaScript | MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/split)**
>
> The split() method of String values takes a pattern and divides this string into an ordered list of substrings by searching for the pattern, puts these substrings into an array, and returns the array.

---

<div class="post-metadata">

**Author:** ![cryosat-physicist-41](https://avatars.discourse-cdn.com/v4/letter/c/278dde/32.png) [@cryosat-physicist-41](https://community.postman.com/u/cryosat-physicist-41)\
**Post date:** [June 18, 2021, 7:57am UTC](https://community.postman.com/t/extract-string-from-response/7473/3 "2021-06-18T07:57:19Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![bpricilla](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/bpricilla/32/6511_2.png) [@bpricilla](https://community.postman.com/u/bpricilla)\
**Post date:** [June 18, 2021, 9:58am UTC](https://community.postman.com/t/extract-string-from-response/7473/4 "2021-06-18T09:58:50Z")

</div>

Hello @cryosat-physicist-41, yes its simple

in the above example,

```auto
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 😊

---

<div class="post-metadata">

**Author:** ![Logan](https://avatars.discourse-cdn.com/v4/letter/l/278dde/32.png) [@Logan](https://community.postman.com/u/Logan)\
**Post date:** [January 7, 2022, 9:29pm UTC](https://community.postman.com/t/extract-string-from-response/7473/5 "2022-01-07T21:29:41Z")

</div>

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](mailto: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](mailto:customer123@example.com)”,  
“priority”: “normal”,  
“status”: “new”  
}  
}

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/system/32/29796_2.png) [@system](https://community.postman.com/u/system)\
**Post date:** [August 24, 2023, 3:10pm UTC](https://community.postman.com/t/extract-string-from-response/7473/6 "2023-08-24T15:10:04Z")

</div>

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