# How to store a JSON in a variable and use it in the request body?

**URL:** <https://community.postman.com/t/how-to-store-a-json-in-a-variable-and-use-it-in-the-request-body/1881>\
**Category:** Help Hub\
**Created:** [June 15, 2018, 7:30am UTC](https://community.postman.com/t/how-to-store-a-json-in-a-variable-and-use-it-in-the-request-body/1881 "2018-06-15T07:30:35Z")\
**Posts on this page:** 20\
**Page:** 2

<div class="post-metadata">

**Author:** ![aakashjain8693](https://avatars.discourse-cdn.com/v4/letter/a/7c8e57/32.png) [@aakashjain8693](https://community.postman.com/u/aakashjain8693)\
**Post date:** [July 29, 2020, 10:33am UTC](https://community.postman.com/t/how-to-store-a-json-in-a-variable-and-use-it-in-the-request-body/1881/22 "2020-07-29T10:33:48Z")

</div>

@singhsivcan @cdxcz Similarly I have one requirement, would like to understand who I move ahead with Postman.  
1.Select the GET Method.  
2. Enter the End point [http://dummy.restapiexample.com/api/v1/employees](http://dummy.restapiexample.com/api/v1/employees) URL.  
3. Click on send button and get the response.  
4. Observe the response.  
5. Verify the response body through test script and data driven approach(.csv file)

Who I can verify those data through script, Yes I know one by one I can verify it.  
but wanted to make script more generic.  
Like put some data into variable, Put same variable and values into .csv file, run the collection run and verify the response.

---

<div class="post-metadata">

**Author:** ![singhsivcan](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/singhsivcan/32/1595_2.png) [@singhsivcan](https://community.postman.com/u/singhsivcan)\
**Post date:** [July 30, 2020, 9:45pm UTC](https://community.postman.com/t/how-to-store-a-json-in-a-variable-and-use-it-in-the-request-body/1881/23 "2020-07-30T21:45:17Z")

</div>

@aakashjain8693 You cannot read a CSV file from the test script unfortunately within the Postman app.  
You might need to use [newman](https://github.com/postmanlabs/newman) to achieve something like this which is our CLI tool and also can be integrated in node.

I am sure @danny-dainton has some ideas around a solution for this kind of problem. 😄

---

<div class="post-metadata">

**Author:** ![aakashjain8693](https://avatars.discourse-cdn.com/v4/letter/a/7c8e57/32.png) [@aakashjain8693](https://community.postman.com/u/aakashjain8693)\
**Post date:** [July 31, 2020, 5:43am UTC](https://community.postman.com/t/how-to-store-a-json-in-a-variable-and-use-it-in-the-request-body/1881/24 "2020-07-31T05:43:44Z")

</div>

@singhsivcan Thanks for the response, Lets wait for @danny-dainton .

---

<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:** [August 10, 2020, 6:29pm UTC](https://community.postman.com/t/how-to-store-a-json-in-a-variable-and-use-it-in-the-request-body/1881/25 "2020-08-10T18:29:27Z")

</div>

Hi @singhsivcan,

I am unable to work with variable, Variable showing null when used `{{temp_token}}`

```auto
var tempvar = console.log(pm.response.json()["temp token"]); 

temp_token = JSON.stringify(tempvar); 

pm.environment.set("temp_token", temp_token); 

```

@danny-dainton your help would be great

---

<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 10, 2020, 6:48pm UTC](https://community.postman.com/t/how-to-store-a-json-in-a-variable-and-use-it-in-the-request-body/1881/26 "2020-08-10T18:48:55Z")

</div>

Hey @gpub1

You might to also need show the response body and how you’re using that variable in the app.

It’s difficult to know what you’re seeing with just the script you’re using to extract the value 😁

---

<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:** [August 10, 2020, 6:52pm UTC](https://community.postman.com/t/how-to-store-a-json-in-a-variable-and-use-it-in-the-request-body/1881/27 "2020-08-10T18:52:00Z")

</div>

🙂 yes, kind a new guy on postman…

```auto
{
    "temp token": "45351467"
}

```

This is the response body 😃

---

<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 10, 2020, 7:09pm UTC](https://community.postman.com/t/how-to-store-a-json-in-a-variable-and-use-it-in-the-request-body/1881/28 "2020-08-10T19:09:36Z")

</div>

Your response body looks quite basic, don’t see many keys with spaces but that reference should work.

Before running, ensure that you have created and selected an environment file.

```auto
let tempVar = pm.response.json()["temp token"]; 

pm.environment.set("temp_token", tempVar); 

```

Add that to the Tests script of the request, the `tempVar` should then be available for you to use.

---

<div class="post-metadata">

**Author:** ![SabriH](https://avatars.discourse-cdn.com/v4/letter/s/9fc348/32.png) [@SabriH](https://community.postman.com/u/SabriH)\
**Post date:** [August 10, 2020, 7:15pm UTC](https://community.postman.com/t/how-to-store-a-json-in-a-variable-and-use-it-in-the-request-body/1881/29 "2020-08-10T19:15:54Z")

</div>

I believe the behaviour you’re observing is explained here: [javascript - Why does console.log say undefined, and then the correct value? - Stack Overflow](https://stackoverflow.com/a/24342788/5348913)

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

---

<div class="post-metadata">

**Author:** ![gsing](https://avatars.discourse-cdn.com/v4/letter/g/90db22/32.png) [@gsing](https://community.postman.com/u/gsing)\
**Post date:** [August 10, 2020, 10:22pm UTC](https://community.postman.com/t/how-to-store-a-json-in-a-variable-and-use-it-in-the-request-body/1881/30 "2020-08-10T22:22:06Z")

</div>

Thanks Danny… Its working now… and skipped this line `temp_token = JSON.stringify(tempvar); `

---

<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 10, 2020, 10:25pm UTC](https://community.postman.com/t/how-to-store-a-json-in-a-variable-and-use-it-in-the-request-body/1881/31 "2020-08-10T22:25:02Z")

</div>

Wasn’t really sure why you even had it in there 🤔

Glad you got it working. 🏆

---

<div class="post-metadata">

**Author:** ![aakashjain8693](https://avatars.discourse-cdn.com/v4/letter/a/7c8e57/32.png) [@aakashjain8693](https://community.postman.com/u/aakashjain8693)\
**Post date:** [August 12, 2020, 12:31pm UTC](https://community.postman.com/t/how-to-store-a-json-in-a-variable-and-use-it-in-the-request-body/1881/32 "2020-08-12T12:31:30Z")

</div>

Any update @danny-dainton, how I can achieve with Nested array or objects?

---

<div class="post-metadata">

**Author:** ![aakashjain8693](https://avatars.discourse-cdn.com/v4/letter/a/7c8e57/32.png) [@aakashjain8693](https://community.postman.com/u/aakashjain8693)\
**Post date:** [August 12, 2020, 1:36pm UTC](https://community.postman.com/t/how-to-store-a-json-in-a-variable-and-use-it-in-the-request-body/1881/33 "2020-08-12T13:36:55Z")

</div>

@allenheltondev looped you to find out the best result for us.

---

<div class="post-metadata">

**Author:** ![allenheltondev](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/allenheltondev/32/31738_2.png) [@allenheltondev](https://community.postman.com/u/allenheltondev)\
**Post date:** [August 12, 2020, 2:59pm UTC](https://community.postman.com/t/how-to-store-a-json-in-a-variable-and-use-it-in-the-request-body/1881/34 "2020-08-12T14:59:37Z")

</div>

Since we seem to be hijacking the thread, it might be better to start a new post with your screenshots and use case stated again.

I think we’ll get better traction that way.

---

<div class="post-metadata">

**Author:** ![aakashjain8693](https://avatars.discourse-cdn.com/v4/letter/a/7c8e57/32.png) [@aakashjain8693](https://community.postman.com/u/aakashjain8693)\
**Post date:** [August 12, 2020, 3:15pm UTC](https://community.postman.com/t/how-to-store-a-json-in-a-variable-and-use-it-in-the-request-body/1881/35 "2020-08-12T15:15:20Z")

</div>

@allenheltondev WIll do, Will give all required details.

---

<div class="post-metadata">

**Author:** ![smaddirala9571](https://avatars.discourse-cdn.com/v4/letter/s/bc8723/32.png) [@smaddirala9571](https://community.postman.com/u/smaddirala9571)\
**Post date:** [September 25, 2020, 7:49pm UTC](https://community.postman.com/t/how-to-store-a-json-in-a-variable-and-use-it-in-the-request-body/1881/36 "2020-09-25T19:49:48Z")

</div>

What if I have a payload like this set in the globals file:  
[{“x”:“abc”,“y”:“efg”,“z”:“hij”}]

But let’s say I want to only access “y” value inside a JSON RAW Body?  
Is that possible? I looked at solutions to escape the “” but I’m struggling.

---

<div class="post-metadata">

**Author:** ![allenheltondev](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/allenheltondev/32/31738_2.png) [@allenheltondev](https://community.postman.com/u/allenheltondev)\
**Post date:** [September 28, 2020, 11:37am UTC](https://community.postman.com/t/how-to-store-a-json-in-a-variable-and-use-it-in-the-request-body/1881/37 "2020-09-28T11:37:46Z")

</div>

In this case, I would use a prerequest script to scope down the variable you want.

```auto
const globalValues = JSON.parse(pm.globals.get('myPayload'));
pm.variables.set('body', globalValues.y);

```

In the body of your request, you can then reference this value simply by adding in `{{body}}`

---

<div class="post-metadata">

**Author:** ![mschuijer](https://avatars.discourse-cdn.com/v4/letter/m/91b2a8/32.png) [@mschuijer](https://community.postman.com/u/mschuijer)\
**Post date:** [February 4, 2021, 6:29pm UTC](https://community.postman.com/t/how-to-store-a-json-in-a-variable-and-use-it-in-the-request-body/1881/38 "2021-02-04T18:29:02Z")

</div>

Hi @singhsivcan or other enthusiasts,

I’ve been reading up for the past 3 days and trying out all sorts of suggestions and pieces of code shared here but can’t seem to get things to work as I want. The last I tried was @Sandokhan 's example of “cities”.

I would appreciate anyone’s feedback here.

I am requesting the azure devops service to get a list of projects in our organization and want to re-use the project names in the response in a next request where I need the names to get a another list of what really want to have.

Example response output:

```
{
    "count": 123,
    "value": [
        {
            "id": "11erui332-1323-4ed3-23r123d4",
            "name": "PROJECT-NAME",
            "url": "https://dev.azure.com/myorg/_apis/projects/id-of-project",
            "state": "wellFormed",
            "revision": 321,
            "visibility": "private",
            "lastUpdateTime": "2020-05-19T14:04:12.69Z"
        },

```

The “name” value is what I am after. I would like to get all the names and use them in the next request, inserted in the {{project}} variable:

`https://dev.azure.com/{{organization}}/{{project}}/_apis/distributedtask/queues?api-version=6.0`

pm.collectionVariables.set() and pm.collectionVariables.get() seem the way to go, but I’m missing something I’m sure.

If there’s an example out there I’ve missed let me know please.

Thanks a lot for any pointers, I’m totally new at this. I did manage to get out nice lists and formatted them in tabular output with the visualization, very nice.

---

<div class="post-metadata">

**Author:** ![singhsivcan](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/singhsivcan/32/1595_2.png) [@singhsivcan](https://community.postman.com/u/singhsivcan)\
**Post date:** [February 7, 2021, 12:42pm UTC](https://community.postman.com/t/how-to-store-a-json-in-a-variable-and-use-it-in-the-request-body/1881/39 "2021-02-07T12:42:19Z")

</div>

Hey @mschuijer, since I can see that you’ve an array of `value` in the response, and you only want the first `name` for your next request?

I think what you want is the following **test script** (make sure it’s NOT put under the pre-request script):

```javascript
let projectName = _.get(pm.response.json(), 'value.0.name');

pm.collectionVariables.set('project', projectName);

```

Now when you run the next request then it should have the correct project name resolved from the collection variable.

---

<div class="post-metadata">

**Author:** ![mschuijer](https://avatars.discourse-cdn.com/v4/letter/m/91b2a8/32.png) [@mschuijer](https://community.postman.com/u/mschuijer)\
**Post date:** [February 8, 2021, 9:23am UTC](https://community.postman.com/t/how-to-store-a-json-in-a-variable-and-use-it-in-the-request-body/1881/40 "2021-02-08T09:23:19Z")

</div>

Thank you for the response.  
I actually want to save all the (135) project names and use each one in the next request:  
`https://dev.azure.com/{{organization}}/{{project}}/_apis/distributedtask/queues?api-version=6.0-preview.1`

I don’t know if that’s possible though? The request has only a place for one {{project}} (name) and I wouldn’t know how this would be able to create on output result for all project names.

Thanks again

---

<div class="post-metadata">

**Author:** ![sonujain](https://avatars.discourse-cdn.com/v4/letter/s/cdc98d/32.png) [@sonujain](https://community.postman.com/u/sonujain)\
**Post date:** [April 16, 2021, 12:58pm UTC](https://community.postman.com/t/how-to-store-a-json-in-a-variable-and-use-it-in-the-request-body/1881/41 "2021-04-16T12:58:00Z")

</div>

Hi Sivcan, Experts,

I have been trying to develop a piece of code to store the post request output(in json format) in an array and compare the array output with a value to see if it matches my condition.

Requirement:  
POST request on [https://frxxxxxxxxxxxxxxxxxxxs.cxxxxxxxCOxxxxxxxxxNG](https://frxxxxxxxxxxxxxxxxxxxs.cxxxxxxxCOxxxxxxxxxNG)  
Bearer Token

parse the answer

{  
“CE:get\_version”: {  
“return\_code”: 0,  
“message”: “DONE”,  
“elapse”: 78  
},  
“CE:get\_status”: {  
“return\_code”: 0,  
“message”: “DONE”,  
“elapse”: 390  
}  
}   
check that all “return\_code” entries are set to 0.  
Beware that the number of entries is dynamic and can vary in time, do not assume a fixed content (e.g below answer shows two entries CE:get\_version and CE:get\_status, but the project will incrementally add some with new names over time without altering the monitoring scenario).

In case any of the return\_code is != 0, trigger an error message that includes the full json answer.

Could you please suggest on this. Thanks in advance.

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

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