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

**URL:** <https://community.postman.com/t/how-to-store-a-json-in-a-variable-and-use-it-in-the-next-request-body/31821>\
**Category:** Help Hub\
**Created:** [January 24, 2022, 12:23am UTC](https://community.postman.com/t/how-to-store-a-json-in-a-variable-and-use-it-in-the-next-request-body/31821 "2022-01-24T00:23:35Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![aafreenss](https://avatars.discourse-cdn.com/v4/letter/a/3ec8ea/32.png) [@aafreenss](https://community.postman.com/u/aafreenss)\
**Post date:** [January 24, 2022, 12:23am UTC](https://community.postman.com/t/how-to-store-a-json-in-a-variable-and-use-it-in-the-next-request-body/31821/1 "2022-01-24T00:23:35Z")

</div>

I have a response of a API which looks like this on a GET - GET on API1  
[

```
{

    "id": "a2d043614c9fefcae1ce9c83a3d0a448821e3a1734d5e0162aa8692c758b44ef",

    "isLastUsed": false,

    "userId": "f7e0e51b-e750-4523-97cf-bb40a8169c76",

    "family": "wallet",

    "type": "wallet_setel"

},

{

    "id": "a2d041614c9feacae1ce9cd3f181a046d74c344134d2e0162aab3c2527d945e1",

    "isLastUsed": false,

    "userId": "f7e0e51b-e750-4523-97cf-bb40a8169c76",

    "family": "mesra_card",

    "type": "mesra_card",

    "card": {

        "cardNumber": "70838155117203383",

        "cardType": "",

        "pointBalance": 1023,

        "issuedBy": "setel",

        "provider": "SETEL",

        "isActive": false,

        "status": "issued"

    }

}

```

]

I would like to extract only the id and provided it as a parameter to API 2  
https://{{baseurl}}/v1/payments/payment-methods/ **{{id}}** /balance

can anyone please guide me.

---

<div class="post-metadata">

**Author:** ![bpricilla](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/bpricilla/32/6511_2.png) [@bpricilla](https://community.postman.com/u/bpricilla)\
**Post date:** [January 24, 2022, 4:05am UTC](https://community.postman.com/t/how-to-store-a-json-in-a-variable-and-use-it-in-the-next-request-body/31821/2 "2022-01-24T04:05:04Z")

</div>

@aafreenss Welcome to the Community 🥳

This is quite a common question in our forum 😊

Can you please refer to the below post if this helps: [How to loop through array and use its values in a request](https://community.postman.com/t/how-to-loop-through-array-and-use-its-values-in-a-request/6771)

So the process goes this way:

1. Since ID is an array, you should store it in an array
2. And figuring out a way to iterate the value over the params (it can even be the traditional for loop)

---

<div class="post-metadata">

**Author:** ![aafreenss](https://avatars.discourse-cdn.com/v4/letter/a/3ec8ea/32.png) [@aafreenss](https://community.postman.com/u/aafreenss)\
**Post date:** [January 24, 2022, 10:26pm UTC](https://community.postman.com/t/how-to-store-a-json-in-a-variable-and-use-it-in-the-next-request-body/31821/3 "2022-01-24T22:26:18Z")

</div>

Hi,  
Thanks for the link to post and quick reply.  
I tried going though the post but still looks bit complicated to me as i am really just starting to learn.  
it would be great if you can explain using my example as to what to write in test for 1st API and pre-request scripts for next API?

any help would be much appreciated.  
thanks

I did try to write below for loop, in the test of 1st API . but once I execute 2nd API, i am only getting the details for family :wallet and not for mesra\_card  
var response = pm.response.json();

for (var i=0; i \< response.length; i++) {

if(response[i].family.includes(“wallet” || “mesra\_card”)) {

```
   pm.collectionVariables.set("id", response[i].id);

   break;

```

}

}

---

<div class="post-metadata">

**Author:** ![xavier.gutierrez](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/xavier.gutierrez/32/10944_2.png) [@xavier.gutierrez](https://community.postman.com/u/xavier.gutierrez)\
**Post date:** [January 25, 2022, 2:18pm UTC](https://community.postman.com/t/how-to-store-a-json-in-a-variable-and-use-it-in-the-next-request-body/31821/4 "2022-01-25T14:18:31Z")

</div>

Hi @aafreenss  
I would store the whole JSON in the variable. You can then get it back and address it as a JSON object.  
I would do:  
`pm.collectionVariables.set("API1-Response",response);`

And later:  
`var api1Response = pm.collectionVariables.get("API1-Response");`

Cheers,  
Xavier.

---

<div class="post-metadata">

**Author:** ![tpagett-finxact](https://avatars.discourse-cdn.com/v4/letter/t/bbe5ce/32.png) [@tpagett-finxact](https://community.postman.com/u/tpagett-finxact)\
**Post date:** [January 25, 2022, 10:38pm UTC](https://community.postman.com/t/how-to-store-a-json-in-a-variable-and-use-it-in-the-next-request-body/31821/5 "2022-01-25T22:38:10Z")

</div>

My process when I have to loop through an array and use specific pieces of information from the current element is as follows.

1. Save the array of information you need. That can be as simple as

```auto
pm.environment.set("arrayOfValues", arr)

```

Or, more frequently I pair down the information from the array that I’m saving.

```auto
let arrValues = [ 
  { 
    name: "First",
    id: "v1"
  },
  {
    name: "Second",
    id: "v2"
  }
];

```

In this example, I only care about the ID, so I use Array.map() to perform a function on each element of the array, to get the data I need.

```auto
let newArray = arrValues.map(e=>{return e.id});

```

This returns an array that looks like this [“v1”, “v2”]

Then, I will save the newArray

```auto
pm.environment.set("arrayOfIds", newArray);

```

Looping through the array is a little odd to wrap your head around the first few times. One of the ways I’ve done it is listed below

Do this right after saving your “arrayOfIds”

```auto
pm.environment.set("currentId", newArray[0])

```

Then, your next api call can reference {{currentId}} in the URL.  
In the post-request tab, you will want to do this, as this is the key that actually forces the looping, but only when done in a runner (newman or through the app’s runner).

```auto
let arrayOfIds = pm.environment.get("arrayOfIds")
let currentId = pm.environment.get("currentId");
let currentIndex = arrayOfIds.indexOf(currentId);

if ( currentIndex < ( arrayOfIds.length() - 1) ) {
  postman.setNextRequest( pm.info.requestId );
  pm.environment.set("currentId", arrayOfIds[++currentIndex]);
}

```

This will loop through an array of string Ids of any size, and repeat the same postman api call for each id.
