top of page

Reading Json response via test script in Postman

If you haven't created your test request, refer Creating collections and test requests in postman.

For creating environment variable and access them in test requests, refer this link.

You can see "Pre-request Script" and "Tests" tab under endpoint textbox to write javascript code to do any action we need to perform.

Pre-request Script: The code written under this section will be executed before the request is run.

Tests: The code under this tab executes after the test request is run.

You can check to work on first two scenarios of this public API in postman in this link.

Scenario 3: Draw a card/cards from the deck

Create a new request named "draw" and add this URL

This URL helps to draw a card or cards from the created deck. The number of cards to be drawn are specified by the variable "count".

Here deck_id is a parameter which needs to be parameterized in order to identify from which deck the cards should be drawn. The deck id should be retrieved from previous request.

In our previous scenario, we have created a new deck using the following URL,

and the response was,

 "success": true,
 "deck_id": "8zn3gs07wjb1",
 "remaining": 52,
 "shuffled": false

We need to take the deck_id from this response, and store it in a variable to use it later.

To do that, enter the following code in Tests tab.

let jsonresponse = pm.response.json();

Code Explanation:

1. In our code the first line is going to read the Json response and store it in a variable.

2. And the second line creates a variable in Environment, and get the value from Json response and store it in that variable.

Now, once the test request is executed, you can see the variable is created and value is reflected in environment.

Now to use that variable in your next test request,


use this syntax in your test request to use the variable created in environment.

Now in place of deck_id, it is parametrized as {{deck_id}} and in response the deck_id from previous test request is retrieved and two cards are drawn from the deck.

Scenario 4: Discard the card

Create a new request named "discard" and add this URL

This above request is used to discard the mentioned cards in URL from a specified deck.

Here we have to pass two values,

  1. deck_id to specify from which deck cards needs to be discarded.

  2. cards as query parameter to specify which cards needs to be discarded.

deck_id will be passed similar to our previous scenario.

Now, we are gonna pass the cards from our previous response. To do that, enter the following code under Tests tab,

let jsonresponse = pm.response.json();

The second line get the cards from previous response, create a environmental variable "cards" and assign the value from response.

Now the variable is created and value is assigned.

the value can be retrieved by mentioning {{cards}} in request URL.

As we execute the test, you can see cards are discarded from the deck successfully.

Happy Testing!!!

174 views0 comments

Recent Posts

See All

Smallest String With A Given Numeric Value

The numeric value of a lowercase character is defined as its position (1-indexed) in the alphabet, so the numeric value of a is 1, the numeric value of b is 2, the numeric value of c is 3, and so on.


bottom of page