5 min read

Testing a nested object REST API response using PostMan

Testing a nested object REST API response using PostMan
Photo by Mika Baumeister / Unsplash

Searching through the internet on different blogs for a test method that deals with
nested objects we couldn’t find something that fits our context. What we needed was an automatic
testing framework using tools like Postman, jMeter or Azure for REST API, Odata and nonOdata compliant.
We would like to share with you how we managed to build some tests for nested objects, with
verification and getting variables from the response body, variables that can be used for future
requests.

Generally speaking API calls retrieve single objects and array objects. For the latter, if we talk about nested objects testing, there will be the need to verify that
what is inside of each nested object it is compliant with the business or other rules that your app might have. We did this using Postman. To make sure that the object from the
response body is exactly as you would expect, after reading this article, you will be able to build a simple testing method that
will deal with this kind of test case. Let’s check the following example of an array of nested
objects, more exactly a JSON response body from Postman:

[
	{
	"id": 963,
	"name": "Module PS",
	"permissionName": null,
	"url": "/module-ps/",
	"parentId": null,
	"categoryId": 1,
	"sortOrder": 0,
	"createdBy": "Admin357",
	"modifiedBy": "Admin357",
	"isDeleted": false,
	"action": "Get",
	"category": {
	"id": 1,
	"categoryName": "Module",
	"createdOn": "2019-07-25",
	"createdBy": "System",
	"modifiedOn": "2019-07-25",
	"modifiedBy": "System",
	"isDeleted": false,
	"permission": [] 
	}
	},
	{
	"id": 453,
	"name": "Module HS",
	"permissionName": null,
	"url": "/module-hs/",
	"parentId": null,
	"categoryId": 1,
	"sortOrder": 0,
	"createdBy": "leonida",
	"modifiedBy": "leonida",
	"isDeleted": false,
	"action": "Get",
	"category": {
	"id": 1,
	"categoryName": "Module",
	"createdOn": "2019-07-25",
	"createdBy": "System",
	"modifiedOn": "2019-07-25",
	"modifiedBy": "System",
	"isDeleted": false,
	"permission": []
	}
	},
	{
	"id": 333,
	"name": "Module RS",
	"permissionName": null,
	"url": "/module-rs/",
	"parentId": null,
	"categoryId": 1,
	"sortOrder": 0,
	"createdBy": "Admin10",
	"modifiedBy": "Admin10",
	"isDeleted": false,
	"action": "Get",
	"category": {
	"id": 1,
	"categoryName": "Module",
	"createdOn": "2019-07-25",
	"createdBy": "System",
	"modifiedOn": "2019-07-25",
	"modifiedBy": "System",
	"isDeleted": false,
	"permission": []
	}
	}
]

Let’s think of those three objects as parent objects with few children for each one. A starting
point will be parsing the JSON file using the method pm.response.json() to fetch the needed
values of each nested object. The method in this case for the example from above will be
something like this

pm.test ( "Module PS check" , function () {
	var jsonData = pm.response.json();
	pm.expect(jsonData[0].id).to.eql(963);
	pm.expect(jsonData[0].name).to.eql("Module PS");
	pm.expect(jsonData[0].permissionName).to.eql(null);
	pm.expect(jsonData[0].url).to.eql("/module-ps/");
	pm.expect(jsonData[0].parentId).to.eql(null);
	pm.expect(jsonData[0].isDeleted).to.eql(false);
	pm.expect(jsonData[0].action).to.eql("Get");
});

pm.test ( "Module HS check" , function () {
	var jsonData = pm.response.json();
	pm.expect(jsonData[1].id).to.eql(453);
	pm.expect(jsonData[1].name).to.eql("Module HS");
	pm.expect(jsonData[1].permissionName).to.eql(null);
	pm.expect(jsonData[1].url).to.eql("/module-hs/");
	pm.expect(jsonData[1].parentId).to.eql(null);
	pm.expect(jsonData[1].isDeleted).to.eql(false);
	pm.expect(jsonData[1].action).to.eql("Get");
});

pm.test ( "Module RS check" , function () {
	var jsonData = pm.response.json();
	pm.expect(jsonData[2].id).to.eql(333);
	pm.expect(jsonData[2].name).to.eql("Module RS");
	pm.expect(jsonData[2].permissionName).to.eql(null);
	pm.expect(jsonData[2].url).to.eql("/module-rs/");
	pm.expect(jsonData[2].parentId).to.eql(null);
	pm.expect(jsonData[2].isDeleted).to.eql(false);
	pm.expect(jsonData[2].action).to.eql("Get");
});

First, we will have to parse the JSON response in a variable var jsonData = pm.response.json();
then check each important property of the nested object using Postman method
pm.expect(jsonData[0].id).to.eql( 963 ); , where [0] represents the position of the first nested object.
Another method will be using environment variables: fetching values from nested objects into an
environment variable, then compare it with an expected value:

pm.test("Get environment variables", function () {
	let jsonData = pm.response.json();
	if ( jsonData.length > 0 ) {
	pm.environment.set('ModulePSid',jsonData[0].id);
	pm.environment.set('ModuleRSid',jsonData[2].id);
	}
});

pm.test("Compare environment variables with an expected value" , function () {
	pm.expect(pm.environment.get('ModulePSid')).to.equal(963);
	pm.expect(pm.environment.get('ModuleRSid')).to.equal(333);
});

As a brief explanation:
jsonData[0].id – nested object no. 0 (first object) and the attribute “id”;
jsonData[4].url – 5th nested object in the JSON file and the attribute

If you would like to check the attributes in a child nested object, in our example:

{
  "id": 963,
  "name": "Module PS",
  "permissionName": null,
  "url": "/module-ps/",
  "parentId": null,
  "categoryId": 1,
  "sortOrder": 0,
  "createdBy": "Admin357",
  "modifiedBy": "Admin357",
  "isDeleted": false,
  "action": "Get",
  "category": {
  "id": 1,
  "categoryName": "Module",
  "createdOn": "2019-07-25",
  "createdBy": "System",
  "modifiedOn": "2019-07-25",
  "modifiedBy": "System",
  "isDeleted": false,
  "permission": []
  }
}

Nested object into another nested object, you will have to parse the attributes like this in a form like this one jsonData[4].category.isDeleted

The following block of code will show you how :

pm test ( "Category ID check" , function () {
	var jsonData = pm.response.json ();
	pm.expect(jsonData[1].category.id ).to.eql(1);
});
// get environment variable from a nested child object
pm.test ( "Get environment variables" , function () {
	let jsonData = pm.response.json();
	if ( jsonData.length > 0 ) {
	pm.environment . set ( 'CategoryID', jsonData[0].category.id);
	}
});

Hope you enjoyed this step by step walkthrough on how to test a REST API response. If you have questions feel free to reach out to us.