APITesting Ezine – Volume 1.2 : How to extract JSON object response in Postman

Let us define the problem statement

We have a REST server managing user registrations. On doing a GET request the response returns a list of users with their id’s, name, surname and address. The response is in JSON format and we want to find out

  1. Number of users who are already registered
  2. The id’s of all the registered users

Please see the following image and the JSON code below

Little bit about JSON

JSON objects are surrounded by curly braces {}. To read more please see the image below.

Another important thing to note is about Arrays in JSON.

In our problem statement – The json object is actually an array of users having key / value pairs for “id”, “name”,”surname”,”adress” -> id is integer rest all are string

Reading and parsing the JSON object in Postman.

In the test script tab of Postman, Postman API has methods to read the response in either string or JSON object format.

The pm.response.json() – is the function which returns Json object for the entire response.

Please see image below which is written in the test script.

The code is simple – and reproduced here.

let jsonData = pm.response.json();

Now to get the length of the user array is simple.

jsonData.user.length // this will give you the length of the user array

console.log ("user array length is = " + jsonData.user.length); // will print the user array length on the console

Here are some more code snippets to go through the first array key value pairs. It is important that the key is case and spelling sensitive when we try to get the value.

console.log ("First user id  = " + jsonData.user[0].id);

console.log ("First user name  = " + jsonData.user[0].name);

console.log ("First user surname  = " + jsonData.user[0].surname);

console.log ("First user address  = " + jsonData.user[0].adress);

If you want to go through the entire user list and print the id’s etc we can use a for loop for the same. code snippet is below

for (var i = 0; i < jsonData.user.length; i++) {
    console.log ("User id for [" + i+ "]" +"= " + jsonData.user[i].id);
    console.log ("User name  for [" + i+ "]" +"= " + jsonData.user[i].name);
    console.log ("User surname for [" + i+ "]" +"= " + jsonData.user[i].surname);
    console.log ("User address  for [" + i + "]" +"= " + jsonData.user[i].adress);

}

The entire code is reproduced below.

pm.test("Status code is 200", function () {
    pm.response.to.have.status(200);
});

let jsonData = pm.response.json();

console.log ("user array length is = " + jsonData.user.length);

// print the details for the first user

console.log ("First user id  = " + jsonData.user[0].id);
console.log ("First user name  = " + jsonData.user[0].name);
console.log ("First user surname  = " + jsonData.user[0].surname);
console.log ("First user address  = " + jsonData.user[0].adress);

// print it in a loop

for (var i = 0; i < jsonData.user.length; i++) {
    console.log ("User id for [" + i+ "]" +"= " + jsonData.user[i].id);
    console.log ("User name  for [" + i+ "]" +"= " + jsonData.user[i].name);
    console.log ("User surname for [" + i+ "]" +"= " + jsonData.user[i].surname);
    console.log ("User address  for [" + i + "]" +"= " + jsonData.user[i].adress);

}

The best way to try is to experiment more. Try to read different JSON objects and their values.

Why do we need to read these values

  1. To validate the same
  2. Very important to store them in some variables which can be consumed in subsequent requests

API call chaining will require us to store some of the values in the upstream API calls and then consume them in the downstream API’s.

Hope this helps you in your API testing pursuit. In the next edition of the Ezine we will try to cover how to play with JSON responses using Rest Assured.

Stay tuned.

References

If you want to learn and get certified in web services testing / API testing. One of the most needed skills for every tester – please do not miss our upcoming CP-WST learning events . Keep a watch on ATAEvents.org

CP-WST is one of its kind global certification which is helping people in the areas of cutting edge area of Web Services / API Testing using most in demand tools like Postman and Rest Assured.

Leave A Reply

Your email address will not be published. Required fields are marked *