Store IDs from json and re-use them in a second API call

array
#1

Hi,

I’m struggling on something that might be easy for some of you.

My goal is to set all the IDs from a JSON file in a variable that I could reuse in a second API call.

In my first call, I get the following result:

{
“items”: [
{
“id”: 96047,
“name”: “my first test”
},
{
“id”: 96048,
“name”: “my second test”
}
],
“total”: 2
}

In the second call to the API, I’d like to give the IDs (96047 and 96048) as parameters. So each time I run the first call, the result set would be stored into a variable called by the second API call.

The second API is formatted the following way:
{
“ids”: [
“96047”,
“96048”
],
“type”: “test”
}

Any hint would be greatly appreciated.

#2

Hi @francoisc,
Welcome to the community :wave:

You can make use of Postman’s variables and collections for this. Create a collection with your two requests (https://learning.getpostman.com/docs/postman/collections/intro_to_collections/)

In your first request’s test script, you can have the following snippet.

let response = pm.response.json(),
    ids = _.map(response.items, ({ id }) => ( id )); // This will store your ids in an array. For reference (https://lodash.com/docs/4.17.11#map)

pm.variables.set('ids', JSON.stringify(ids));

And in the body of the second request, you can access the ids like this. Choose Raw and content type as application/json

{
 "ids": {{ids}},
 "type": "test"
}

Now you can run your collection with the help of Postman’s collection runner (https://learning.getpostman.com/docs/postman/collection_runs/starting_a_collection_run/)

The second request body will have your ids. You can check the requests sent in Postman’s console (View > Postman Console)

1 Like
#3

Thank you :grinning:

The result is somewhat not correct…

“com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of java.lang.Long out of START_OBJECT token at [Source: {“ids”:[{“id”:96047},{“id”:96048},{“id”:96049},{“id”:96050},{“id”:96052},{“id”:96051},{“id”:96054},{“id”:96055},{“id”:96056},{“id”:96057},{“id”:96058},{“id”:96059},{“id”:96030},{“id”:96044},{“id”:96045},{“id”:96060},{“id”:96061},{“id”:96062},{“id”:96063},{“id”:96064},{“id”:96065},{“id”:96066},{“id”:96067},{“id”:96068},{“id”:96069},{“id”:96070},{“id”:96071},{“id”:96072},{“id”:96073},{“id”:96074},{“id”:96075},{“id”:96076},{“id”:96077},{“id”:96078},{“id”:96079},{“id”:96080},{“id”:96081},{“id”:96082},{“id”:96083},{“id”:96084},{“id”:96085},{“id”:96086},{“id”:96087},{“id”:96088},{“id”:96089},{“id”:96090},{“id”:96091},{“id”:96092},{“id”:96093},{“id”:96094},{“id”:96095},{“id”:96096},{“id”:96097},{“id”:96098},{“id”:96099},{“id”:96100},{“id”:96101},{“id”:96102},{“id”:96103},{“id”:96104}],“type”:“test”}; line: 1, column: 9]”

#4

Hey @francoisc,
Looks like your API is expecting ids in this format [96047, 96048....]. The snippet earlier sends it in [{id: 96047, id: 96048}] format. You can check my edited answer to achieve the required format instead