API Testing with Postman – extract value from array

By: Slav Kurochkin

In API Testing with Postman from time to time I need to get one specific value from array of values, store and than later make a call. First thing to do is write down requirements using BDD gherkin syntax, than you can easy understand what you expecting to get and what you wish to extract from returned data.

Here is my JSON example:

{
  "ids": [
    1,
    2,
    3,
    4,
    5
  ],
  "someotherData": 25
}

The test case in gherkin syntax would be something like this:

Scenario: Extract value from JSON array
Given find a key "ids"
When ids is listed select first id "ids[0]"
Then store it in environment variables with name "listOfIds"

And here how you would do it using Postman, basically add snippet to your Postman Tests tab:


if (data.ids) {
  tests["Body has token"] = true;
  postman.setEnvironmentVariable("listOfIds", data.ids[0]);
}
else {
  tests["Body has token"] = false;
}
API Testing with Postman
View Comments

Leave a Comment

Use markdown or basic HTML and be nice.