# Saving a string from POST as a variable

**URL:** <https://community.postman.com/t/saving-a-string-from-post-as-a-variable/32688>\
**Category:** Help Hub\
**Created:** [February 17, 2022, 6:37am UTC](https://community.postman.com/t/saving-a-string-from-post-as-a-variable/32688 "2022-02-17T06:37:16Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![cdolan90](https://avatars.discourse-cdn.com/v4/letter/c/bcef8e/32.png) [@cdolan90](https://community.postman.com/u/cdolan90)\
**Post date:** [February 17, 2022, 6:37am UTC](https://community.postman.com/t/saving-a-string-from-post-as-a-variable/32688/1 "2022-02-17T06:37:16Z")

</div>

Hello. Trying to figure out how to save a string from a post and save it as a variable to use in my next test.

The JSON returns data like so

```auto
"{\"a_gains\": [1.8499999999999996, 20.15, 19.7, 13.275, 9.4, 12.5, 16.59], \"b_gains\": [1.8499999999999996, 20.15, 19.7, 13.275, 9.4, 12.5, 32.59], \"session_id\": "

```

In the past I would look in the json object and find the object and save it, but I’m not sure how to do so with a string. EG If i try something like this

const jsonData = pm.response.json();

pm.test(‘Session\_id is’ + jsonData.session\_id, function() {

pm.expect(jsonData).to.have.property(‘session\_id’);

Then session\_id just comes back as undefined. Is it because It’s not an object in the JSON?  
I know it can see the string. If i do a simple test like so

```auto
pm.test("Body matches string", function () {
    pm.expect(pm.response.text()).to.include("session_id");

```

It tells me the body does match the string.

---

<div class="post-metadata">

**Author:** ![sahuvikramp](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/sahuvikramp/32/10828_2.png) [@sahuvikramp](https://community.postman.com/u/sahuvikramp)\
**Post date:** [February 17, 2022, 9:22am UTC](https://community.postman.com/t/saving-a-string-from-post-as-a-variable/32688/2 "2022-02-17T09:22:20Z")

</div>

Hello @cdolan90, Welcome to the community 🎉

The shared JSON data looks like a string, so try converting to JSON object, and that’s probably the reason you are getting session\_id as undefined.

```auto
var RandData = JSON.parse("{\"a_gains\": [1.8499999999999996, 20.15, 19.7, 13.275, 9.4, 12.5, 16.59], \"b_gains\": [1.8499999999999996, 20.15, 19.7, 13.275, 9.4, 12.5, 32.59], \"session_id\": 1795, \"sound_file\": \"gs://sound-files-test/comparison/8k/comparison_Listen_to_the.wav\", \"step_count\": 1}");

/*
var data = pm.response.json()
*/

/* you can set the collection/environment/global variables (below is collectionVariables)*/
pm.collectionVariables.set("session_id", RandData.session_id)

/*To check whether you have session_id as key or not*/
pm.test("session_id is available in JSON data", function () {
    pm.expect(RandData).to.have.property('session_id');
});

```

And if you are not converting to JSON object and directly checking for **session\_id**.

```auto
pm.test("Body matches string", function () {
    pm.expect(RandData).to.include("session_id");
});

```

All the best 👍 😀

---

<div class="post-metadata">

**Author:** ![cdolan90](https://avatars.discourse-cdn.com/v4/letter/c/bcef8e/32.png) [@cdolan90](https://community.postman.com/u/cdolan90)\
**Post date:** [February 17, 2022, 3:21pm UTC](https://community.postman.com/t/saving-a-string-from-post-as-a-variable/32688/3 "2022-02-17T15:21:59Z")

</div>

Hi Sahuvikramp.

This worked thank you. The issue is that i’m having is session\_id will always return a different number, so in my example it was 1795 but it changes everytime. Is there a way to parse the JSON so it returns that value instead of a hard coded one?

Edit: I’ve found the solution but thank you for pointing me in the right direction.

```auto
let response = pm.response.json();

var RandData = JSON.parse(response);

postman.setEnvironmentVariable("session_id", RandData.session_id);

```
