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

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.

@aafreenss Welcome to the Community :partying_face:

This is quite a common question in our forum :blush:

Can you please refer to the below post if this helps: How to loop through array and use its values in a request

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)
2 Likes

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;

}

}

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.

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
pm.environment.set("arrayOfValues", arr)

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

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.

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

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

Then, I will save the newArray

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”

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).

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.