# Parse Text Output to Variable

**URL:** <https://community.postman.com/t/parse-text-output-to-variable/8573>\
**Category:** Help Hub\
**Created:** [October 25, 2019, 12:18pm UTC](https://community.postman.com/t/parse-text-output-to-variable/8573 "2019-10-25T12:18:40Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![ger0001p](https://avatars.discourse-cdn.com/v4/letter/g/f14d63/32.png) [@ger0001p](https://community.postman.com/u/ger0001p)\
**Post date:** [October 25, 2019, 12:18pm UTC](https://community.postman.com/t/parse-text-output-to-variable/8573/1 "2019-10-25T12:18:40Z")

</div>

Can someone help me with following response:

“Session Token-\> BAMAuthToken: 4HyQcMTU3MjAwMzA5NTkyMzpnZXIwMDAxcF9hcGk ← for User : ger0001p\_api”

I need the token stored in a variable TOKEN. (4HyQcMTU3MjAwMzA5NTkyMzpnZXIwMDAxcF9hcGk)

 ![image](https://us1.discourse-cdn.com/getpostman/original/3X/3/e/3e3970f43098ae513a233e5a6e36be3f063b254a.png)

Thanks and best regards,  
Philipp

---

<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:** [October 25, 2019, 12:54pm UTC](https://community.postman.com/t/parse-text-output-to-variable/8573/2 "2019-10-25T12:54:13Z")

</div>

Hey @ger0001p,

You _could_ give this a go - It’s only going to cover a very basic way of getting and setting that value from the response:

Getting the response body as text using `pm.response.text()`, splitting the data using the [.split()](https://www.w3schools.com/jsref/jsref_split.asp) function and then creating an `array` of those items.

```
console.log(pm.response.text().split(" "))

```

 ![47](https://us1.discourse-cdn.com/getpostman/original/3X/b/4/b4453125214fd373d5b7169436595eddb833c109.png)

The item is `4th` in the zero-indexed list so you can reference that using `[3]` to save that value as an environment variable.

`pm.environment.set("TOKEN", pm.response.text().split(" ")[3])`

 ![27](https://us1.discourse-cdn.com/getpostman/original/3X/6/2/622db1eef417fe2852d498822f665862c3c70ffa.png)

---

<div class="post-metadata">

**Author:** ![ger0001p](https://avatars.discourse-cdn.com/v4/letter/g/f14d63/32.png) [@ger0001p](https://community.postman.com/u/ger0001p)\
**Post date:** [October 28, 2019, 9:15am UTC](https://community.postman.com/t/parse-text-output-to-variable/8573/3 "2019-10-28T09:15:48Z")

</div>

> [@danny-dainton](#):
>
> pm.environment.set(“TOKEN”, pm.response.text().split(" ")[3])

Thank you @danny-dainton. Works perfect! 🙂

---

<div class="post-metadata">

**Author:** ![vdespa](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/vdespa/32/2770_2.png) [@vdespa](https://community.postman.com/u/vdespa)\
**Post date:** [October 29, 2019, 9:26am UTC](https://community.postman.com/t/parse-text-output-to-variable/8573/4 "2019-10-29T09:26:01Z")

</div>

You should really encourage the entity responsible for the API to provide you with a parsable response, like JSON. This is really a nasty hack which can easily break in the future.

---

<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:** [October 29, 2019, 12:56pm UTC](https://community.postman.com/t/parse-text-output-to-variable/8573/5 "2019-10-29T12:56:28Z")

</div>

Agreed, This solution will not stand the test of time.

---

<div class="post-metadata">

**Author:** ![arpit.agrawal](https://avatars.discourse-cdn.com/v4/letter/a/c5a1d2/32.png) [@arpit.agrawal](https://community.postman.com/u/arpit.agrawal)\
**Post date:** [February 20, 2020, 11:42am UTC](https://community.postman.com/t/parse-text-output-to-variable/8573/6 "2020-02-20T11:42:02Z")

</div>

Hi @danny-dainton,

Your given solution is working for me but here I have some more text in the next line before the space which one I don’t want to take in my variable value.

So could you please let me know how can I add 2 split in single function or split text.

Response Body:  
Job Id : 70c33082-326f-4276-88ca-04dcc046ef55  
Total Number of Records: 2  
Total Number of Successful Records: 0  
Total Number of Failure Records: 2  
Total Number of Blank Records: 0

My Script:  
var SearchHelp = pm.response.text().split(" ")[3];

Getting ‘SearchHelp’ Value:  
70c33082-326f-4276-88ca-04dcc046ef55  
Total

and I need only 70c33082-326f-4276-88ca-04dcc046ef55 value in my SearchHelp variable.

---

<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:** [February 20, 2020, 12:15pm UTC](https://community.postman.com/t/parse-text-output-to-variable/8573/7 "2020-02-20T12:15:15Z")

</div>

Hey @arpit.agrawal,

It seems like you have a different response to the original question and the different pieces of data are on new lines, something like this might help you:

```
let splitResponse = pm.response.text().split("\n")

pm.environment.set("SearchHelp", splitResponse[0].split(" ")[3])

```

It looks hacky and it felt hacky even writing this though ☹

As @vdespa mentioned:

> You should really encourage the entity responsible for the API to provide you with a parsable response, like JSON. This is really a nasty hack which can easily break in the future.

---

<div class="post-metadata">

**Author:** ![gpub1](https://avatars.discourse-cdn.com/v4/letter/g/f6c823/32.png) [@gpub1](https://community.postman.com/u/gpub1)\
**Post date:** [November 3, 2020, 2:40pm UTC](https://community.postman.com/t/parse-text-output-to-variable/8573/8 "2020-11-03T14:40:12Z")

</div>

Have a response

`{ Token: ["33908e17-ba40-453e-a630-3de91d6cad72"]}`

`pm.environment.set("Token", pm.response.text().split(" ")[2])`

its is giving me token value as [“33908e17-ba40-453e-a630-3de91d6cad72”]}

Need of only value as 33908e17-ba40-453e-a630-3de91d6cad72. How to get it and print it in console

---

<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:** [November 3, 2020, 3:03pm UTC](https://community.postman.com/t/parse-text-output-to-variable/8573/9 "2020-11-03T15:03:17Z")

</div>

If that’s a JSON response, you would just need to do this:

```auto
console.log(pm.response.json().Token[0])

```

The original question was talking about a response that was text across multiple lines so that solution wouldn’t really work for your response. As it’s JSON, you can just retrieve this in the normal way. 😃

---

<div class="post-metadata">

**Author:** ![gpub1](https://avatars.discourse-cdn.com/v4/letter/g/f6c823/32.png) [@gpub1](https://community.postman.com/u/gpub1)\
**Post date:** [November 3, 2020, 3:05pm UTC](https://community.postman.com/t/parse-text-output-to-variable/8573/10 "2020-11-03T15:05:48Z")

</div>

Hey Danny good to see you back!! sorry should’ve update its Text response  
`{ Token: ["33908e17-ba40-453e-a630-3de91d6cad72"]}`

---

<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:** [November 3, 2020, 3:18pm UTC](https://community.postman.com/t/parse-text-output-to-variable/8573/11 "2020-11-03T15:18:52Z")

</div>

That’s quite an important piece of information to miss out, given the way the response looks.

```auto
console.log(pm.response.text().split('"')[1])

```

Again this is a very hacky way of doing it but it should get down to what you need.

---

<div class="post-metadata">

**Author:** ![gpub1](https://avatars.discourse-cdn.com/v4/letter/g/f6c823/32.png) [@gpub1](https://community.postman.com/u/gpub1)\
**Post date:** [November 3, 2020, 3:40pm UTC](https://community.postman.com/t/parse-text-output-to-variable/8573/12 "2020-11-03T15:40:45Z")

</div>

🙂 thanks my bad again missing out. that works and now pushing that to csv too

````auto
let Lguid = pm.response.text().split('"')[1];
console.log(`,${Lguid}\n`); ```
````
