# Using a previous Postman requests JSON response data for the following Postman request

**URL:** <https://community.postman.com/t/using-a-previous-postman-requests-json-response-data-for-the-following-postman-request/37106>\
**Category:** Help Hub\
**Created:** [July 11, 2022, 7:16pm UTC](https://community.postman.com/t/using-a-previous-postman-requests-json-response-data-for-the-following-postman-request/37106 "2022-07-11T19:16:14Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![teamgyst](https://avatars.discourse-cdn.com/v4/letter/t/d2c977/32.png) [@teamgyst](https://community.postman.com/u/teamgyst)\
**Post date:** [July 11, 2022, 7:16pm UTC](https://community.postman.com/t/using-a-previous-postman-requests-json-response-data-for-the-following-postman-request/37106/1 "2022-07-11T19:16:14Z")

</div>

I have set up a Collection variable in the Test tab of my first Postman request as follows:

const jsonData = pm.response.json();  
pm.collectionVariables.set(‘gSession’, jsonData.gSession);

I hardcode the body for the first Postman request as follows…

"{  
"gSession": "1",  
"gEnd": "1",  
"gReq": "21",…etc  
}

This much works fine. The server responds with a good JSON body.

I want to use the gSession collection variable in the JSON body of the next Postman request as follows…

"{  
"gSession": “{{gSession}},  
"gEnd": "1",  
"gReq": "1",…etc  
}”

But this does not work - the server gives an error.

Any help much appreciated.

---

<div class="post-metadata">

**Author:** ![karelhusa](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/karelhusa/32/12594_2.png) [@karelhusa](https://community.postman.com/u/karelhusa)\
**Post date:** [July 11, 2022, 8:07pm UTC](https://community.postman.com/t/using-a-previous-postman-requests-json-response-data-for-the-following-postman-request/37106/2 "2022-07-11T20:07:24Z")

</div>

@teamgyst,  
please have a look in Postman console to check what is actually sent in your request (the JSON document).

The {{gSession}} variable should be substituted with the actual value.

Also, check if the JSON is well-formed, i.e. does not contain any redundant/missing quotes, commas, etc.

Best regards,  
Karel

---

<div class="post-metadata">

**Author:** ![TestQA2019](https://avatars.discourse-cdn.com/v4/letter/t/a4c791/32.png) [@TestQA2019](https://community.postman.com/u/TestQA2019)\
**Post date:** [July 11, 2022, 8:39pm UTC](https://community.postman.com/t/using-a-previous-postman-requests-json-response-data-for-the-following-postman-request/37106/3 "2022-07-11T20:39:18Z")

</div>

It works for me  
Here is request1

 ![image](https://us1.discourse-cdn.com/getpostman/original/2X/5/5ad36cb04603bb75e17709212a0b5eb1381e423d.png)

Here is request2 with gSession in the body

 ![image](https://us1.discourse-cdn.com/getpostman/original/2X/b/b5ac4ebb9447995ad0477cf619ef7b593e9382bf.png)

---

<div class="post-metadata">

**Author:** ![teamgyst](https://avatars.discourse-cdn.com/v4/letter/t/d2c977/32.png) [@teamgyst](https://community.postman.com/u/teamgyst)\
**Post date:** [July 11, 2022, 9:47pm UTC](https://community.postman.com/t/using-a-previous-postman-requests-json-response-data-for-the-following-postman-request/37106/4 "2022-07-11T21:47:36Z")

</div>

> [@teamgyst](#):
>
> "{  
> “gSession”: “1”,  
> “gEnd”: “1”,  
> “gReq”: “21”,…etc  
> }

Many thanks for the quick reply @karelhusa and @TestQA2019 .

My mistake on the original post. I am actually trying to send a string representing a JSON object to the server, so it looks like this:

"{  
"gSession": "1",  
"gEnd": "1",  
"gReq": "21",…etc

I’m sure the escape chars are interfering with the collection variable representation, though I’m not sure how to use that variable in my particular use case.

Also, I’m relatively new to Postman so I’m not sure how to view the Console window you have in your screen shot @TestQA2019. I’m using POST method and can only see the POST request and response panels/windows.

Thanks again for your kind assistance.

---

<div class="post-metadata">

**Author:** ![w4dd325](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/w4dd325/32/12436_2.png) [@w4dd325](https://community.postman.com/u/w4dd325)\
**Post date:** [July 13, 2022, 3:19pm UTC](https://community.postman.com/t/using-a-previous-postman-requests-json-response-data-for-the-following-postman-request/37106/5 "2022-07-13T15:19:03Z")

</div>

Hi @teamgyst

The console log can be found in the bottom left corner;

![image](https://us1.discourse-cdn.com/getpostman/original/2X/9/9a69792f002beed4229eec9e0419530cbe48ddae.png)

---

<div class="post-metadata">

**Author:** ![teamgyst](https://avatars.discourse-cdn.com/v4/letter/t/d2c977/32.png) [@teamgyst](https://community.postman.com/u/teamgyst)\
**Post date:** [July 13, 2022, 5:16pm UTC](https://community.postman.com/t/using-a-previous-postman-requests-json-response-data-for-the-following-postman-request/37106/6 "2022-07-13T17:16:11Z")

</div>

IDK how I missed that. Many thanks @w4dd325

---

<div class="post-metadata">

**Author:** ![teamgyst](https://avatars.discourse-cdn.com/v4/letter/t/d2c977/32.png) [@teamgyst](https://community.postman.com/u/teamgyst)\
**Post date:** [July 19, 2022, 12:38am UTC](https://community.postman.com/t/using-a-previous-postman-requests-json-response-data-for-the-following-postman-request/37106/7 "2022-07-19T00:38:27Z")

</div>

Just checking back here…any ideas on how I can use the method @TestQA2019 describes above with a string as follows…

"{  
“gSession”: “1”,  
“gEnd”: “1”,  
“gReq”: “21”,…etc  
}  
[/quote]

---

<div class="post-metadata">

**Author:** ![w4dd325](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/w4dd325/32/12436_2.png) [@w4dd325](https://community.postman.com/u/w4dd325)\
**Post date:** [July 19, 2022, 7:30am UTC](https://community.postman.com/t/using-a-previous-postman-requests-json-response-data-for-the-following-postman-request/37106/8 "2022-07-19T07:30:05Z")

</div>

Hi @teamgyst  
The example shown (gSession) was a string.  
Could you be a little more specific with what you mean?

---

<div class="post-metadata">

**Author:** ![teamgyst](https://avatars.discourse-cdn.com/v4/letter/t/d2c977/32.png) [@teamgyst](https://community.postman.com/u/teamgyst)\
**Post date:** [July 19, 2022, 12:36pm UTC](https://community.postman.com/t/using-a-previous-postman-requests-json-response-data-for-the-following-postman-request/37106/9 "2022-07-19T12:36:29Z")

</div>

Hi @w4dd325 and thanks for the reply.

That’s correct, the example I posted has leading and ending quotes, so it is a string. But the example @TestQA2019 posted above does not, as it appears to be JSON. So I wanted to know if the variable is supposed to work either way as it did not seem to work for me with a string.

---

<div class="post-metadata">

**Author:** ![w4dd325](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/w4dd325/32/12436_2.png) [@w4dd325](https://community.postman.com/u/w4dd325)\
**Post date:** [July 19, 2022, 1:47pm UTC](https://community.postman.com/t/using-a-previous-postman-requests-json-response-data-for-the-following-postman-request/37106/10 "2022-07-19T13:47:37Z")

</div>

could you share a screenshot of what you have tried and the console output?

---

<div class="post-metadata">

**Author:** ![w4dd325](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/w4dd325/32/12436_2.png) [@w4dd325](https://community.postman.com/u/w4dd325)\
**Post date:** [July 19, 2022, 5:55pm UTC](https://community.postman.com/t/using-a-previous-postman-requests-json-response-data-for-the-following-postman-request/37106/11 "2022-07-19T17:55:52Z")

</div>

Ohh I just re-read it, you mean that the JSON is in double quotes.  
I couldn’t see the closing double quote in your example.

My first question would be; why is your JSON being passed as a string?

* * *

If it is definitely being passed as a string, then you would probably need to use a regEx and Slice, like this;

```auto
const response = pm.response.text();
let gSession = response.match(/gSession\": \"(.*?)\"\,/gm);
console.log(gSession[0].slice(12, -2));

```

---

<div class="post-metadata">

**Author:** ![teamgyst](https://avatars.discourse-cdn.com/v4/letter/t/d2c977/32.png) [@teamgyst](https://community.postman.com/u/teamgyst)\
**Post date:** [July 19, 2022, 8:29pm UTC](https://community.postman.com/t/using-a-previous-postman-requests-json-response-data-for-the-following-postman-request/37106/12 "2022-07-19T20:29:58Z")

</div>

Hi @w4dd325 and thanks for the code above. I’ll try it out.

The server this goes to requires a string. It receives data via http GET method. I’m curious, does that seem unusual or non-standard to you?

Thanks

---

<div class="post-metadata">

**Author:** ![w4dd325](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/w4dd325/32/12436_2.png) [@w4dd325](https://community.postman.com/u/w4dd325)\
**Post date:** [July 19, 2022, 10:11pm UTC](https://community.postman.com/t/using-a-previous-postman-requests-json-response-data-for-the-following-postman-request/37106/13 "2022-07-19T22:11:26Z")

</div>

> [@teamgyst](#):
>
> I’m curious, does that seem unusual or non-standard to you?

I’m not a JSON expert or anything but It just seemed a little odd that the body would be formatted using JSON but then passed as a string.

Maybe someone with more API/Dev skills could educate me if my thought here is incorrect.

---

<div class="post-metadata">

**Author:** ![teamgyst](https://avatars.discourse-cdn.com/v4/letter/t/d2c977/32.png) [@teamgyst](https://community.postman.com/u/teamgyst)\
**Post date:** [July 19, 2022, 11:48pm UTC](https://community.postman.com/t/using-a-previous-postman-requests-json-response-data-for-the-following-postman-request/37106/14 "2022-07-19T23:48:02Z")

</div>

I was able to get the server to accept JSON and tried it as @TestQA2019 indicated above.

It works fine now.

Again, many thanks @w4dd325 and @TestQA2019

---

<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:** [December 12, 2024, 12:54pm UTC](https://community.postman.com/t/using-a-previous-postman-requests-json-response-data-for-the-following-postman-request/37106/15 "2024-12-12T12:54:13Z")

</div>

Closed on December 12, 2024, 12:54pm UTC

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