# How to store the response body as a JSON variable?

**URL:** <https://community.postman.com/t/how-to-store-the-response-body-as-a-json-variable/68071>\
**Category:** Help Hub\
**Tags:** variables, json, just-getting-started\
**Created:** [September 5, 2024, 3:55pm UTC](https://community.postman.com/t/how-to-store-the-response-body-as-a-json-variable/68071 "2024-09-05T15:55:57Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![jvacon](https://avatars.discourse-cdn.com/v4/letter/j/ecc23a/32.png) [@jvacon](https://community.postman.com/u/jvacon)\
**Post date:** [September 5, 2024, 3:55pm UTC](https://community.postman.com/t/how-to-store-the-response-body-as-a-json-variable/68071/1 "2024-09-05T15:55:57Z")

</div>

Hi Folks!

I’m new to Postman and I have no sweet clue about coding. The only thing I kind of know is Excel VBA 😛

I was following along with a previous question posted in 2018 (linked below) about how to store a JSON in a variable. I was able to get it to work but I don’t know how to clean up the response.

Note that the variable I’m trying to save is the Connection ID so it changes every time, so I’ve set the pre-request to clear the variable before the main call.

Thanks in advance!!! 🙏 😊

**Pre-Request**

> pm.collectionVariables.clear(“connectionID”)

**Body**

POST: [https://stuff.com/](https://stuff.com/)(site share name)/ws/JSONBridge.cfm

> {  
> “arguments”:{  
> “apiKey”:“1234”,  
> “logoutCurrentWebConnection”:false  
> },  
> “methodName”:“connectWithApiKey”,  
> “serviceName”:“ConnectionService”  
> }

**Result**

> {  
> “result”:“(site share name)-(unique session ID)”  
> }

**Post-Response**

> let response = pm.response.json(),  
> connectionID = JSON.stringify(response);  
> pm.collectionVariables.set(“connectionID”, connectionID);

**The saved variable then looks like this**  
connectionID= “result”:“(site share name)-(unique session ID)”  
when I really need it to be this  
connectionID= (site share name)-(unique session ID)

[How to store a JSON in a variable and use it in the request body?](https://community.postman.com/t/how-to-store-a-json-in-a-variable-and-use-it-in-the-request-body/1881)

---

<div class="post-metadata">

**Author:** ![michaelderekjones](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/michaelderekjones/32/14819_2.png) [@michaelderekjones](https://community.postman.com/u/michaelderekjones)\
**Post date:** [September 5, 2024, 4:12pm UTC](https://community.postman.com/t/how-to-store-the-response-body-as-a-json-variable/68071/2 "2024-09-05T16:12:02Z")

</div>

```auto
let response = pm.response.json();
pm.collectionVariables.set("connectionID", response.result);
console.log(pm.collectionVariables.get("connectionID")); // "(site share name)-(unique session ID)"

```

---

<div class="post-metadata">

**Author:** ![jvacon](https://avatars.discourse-cdn.com/v4/letter/j/ecc23a/32.png) [@jvacon](https://community.postman.com/u/jvacon)\
**Post date:** [September 5, 2024, 4:18pm UTC](https://community.postman.com/t/how-to-store-the-response-body-as-a-json-variable/68071/3 "2024-09-05T16:18:29Z")

</div>

Thank you so so much! You’re a wizard!

Question,

What’s the difference between

> let response = pm.response.json();  
> pm.collectionVariables.set(“connectionID”, response.result);  
> console.log(pm.collectionVariables.get(“connectionID”));

> let response = pm.response.json(),
> 
> connectionID = JSON.stringify(response);
> 
> pm.collectionVariables.set(“connectionID”, connectionID);

---

<div class="post-metadata">

**Author:** ![michaelderekjones](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/michaelderekjones/32/14819_2.png) [@michaelderekjones](https://community.postman.com/u/michaelderekjones)\
**Post date:** [September 5, 2024, 4:22pm UTC](https://community.postman.com/t/how-to-store-the-response-body-as-a-json-variable/68071/4 "2024-09-05T16:22:11Z")

</div>

In your original code, you were stringifying the whole response and then storing that as the collection variable.

Where in the code I provided, I’m targetting the result key directly and then storing the value of that key as the collection variable.

---

<div class="post-metadata">

**Author:** ![jvacon](https://avatars.discourse-cdn.com/v4/letter/j/ecc23a/32.png) [@jvacon](https://community.postman.com/u/jvacon)\
**Post date:** [September 5, 2024, 4:23pm UTC](https://community.postman.com/t/how-to-store-the-response-body-as-a-json-variable/68071/5 "2024-09-05T16:23:41Z")

</div>

Ohhhh! Gotcha!

Thank you so very much!

---

<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:** [September 8, 2024, 4:24pm UTC](https://community.postman.com/t/how-to-store-the-response-body-as-a-json-variable/68071/6 "2024-09-08T16:24:10Z")

</div>

Closed on September 8, 2024, 4:24pm UTC

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