Jump to content


Photo

How to build up a set of variables from one element in a json array?

json

  • Please log in to reply
1 reply to this topic

#1 trish.mcdonough

trish.mcdonough

    Newbie

  • Members
  • Pip
  • 3 posts

Posted 27 July 2017 - 10:40 AM

Hello,

 

I'm hoping you can help me - I've been using WAPT ond an off for a number of years and thought I was relatively comfortable with it but this is the first time that the system we've built returns json rather than HTML and it's thrown me.

 

Basically I've got a list screen which it returns a number of course approvals.  For this scenario I would like to open a course approval which is not currently assigned to a user.  I need to build up the variables for the URL for the next request.

 

This is the json I'm getting back in the response:

{
	"result" : {
		"items" : [
			
			{
				"id" : 12,
				"application" : {
					"id" : 4,
					"course" : {
						"id" : 1000003,
						"provider" : {
							"providerName" : "First Ambulance"
						},
						"courseType" : {
							"name" : "First Aid"
						}
					},
					"applicationType" : {
						"name" : "Initial"
					}
				},
				"approvalTypeId" : 1,
				"dueDate" : "2017-08-17",
				"visitDate" : null,
				"assignedToUser" : {
					"firstName" : "John",
					"surname" : "Smith",
					"location" : {
						"name" : "Glasgow Office"
					}
				},
				"approvalStatus" : {
					"name" : "Pending"
				},
				"approvalOutcomeId" : null
			},
			
			{
				"id" : 20,
				"application" : {
					"id" : 11,
					"course" : {
						"id" : 1000010,
						"trainingProvider" : {
							"trainingProviderName" : "First Fire Trust"
						},
						"courseType" : {
							"name" : "Fire Prevention"
						}
					},
					"applicationType" : {
						"name" : "Initial"
					}
				},
				"approvalTypeId" : 1,
				"dueDate" : "2017-08-17",
				"visitDate" : null,
				"assignedToUser" : null,
				"approvalStatus" : {
					"name" : "Pending"
				},
				"approvalOutcomeId" : null
			},
			
			{
				"id" : 22,
				"application" : {
					"id" : 13,
					"shortCourse" : {
						"id" : 1000012,
						"trainingProvider" : {
							"trainingProviderName" : "Business Development Inc"
						},
						"courseType" : {
							"name" : "Accounting"
						}
					},
					"applicationType" : {
						"name" : "Initial"
					}
				},
				"approvalTypeId" : 1,
				"dueDate" : "2017-08-28",
				"visitDate" : null,
				"assignedToUser" : null,
				"approvalStatus" : {
					"name" : "Pending"
				},
				"approvalOutcomeId" : null
			},
			
			{
				"id" : 24,
				"application" : {
					"id" : 14,
					"course" : {
						"id" : 1000012,
						"trainingProvider" : {
							"trainingProviderName" : "Business Development Inc"
						},
						"courseType" : {
							"name" : "Legal Entities"
						}
					},
					"applicationType" : {
						"name" : "Initial"
					}
				},
				"approvalTypeId" : 1,
				"dueDate" : "2017-08-28",
				"visitDate" : null,
				"assignedToUser" : null,
				"approvalStatus" : {
					"name" : "Pending"
				},
				"approvalOutcomeId" : null
			}
		],
		"count" : 4,
		"pageCount" : 1,
		"totalItemCount" : 4,
		"pageNumber" : 1,
		"pageSize" : 25,
		"hasPreviousPage" : false,
		"hasNextPage" : false,
		"isFirstPage" : true,
		"isLastPage" : true,
		"firstItemOnPage" : 1,
		"lastItemOnPage" : 4
	},
	"notifications" : [],
	"errors" : [],
	"succeeded" : true,
	"failed" : false
}

As you can see there are 4 course approvals being returned.  The first one is assigned to a user (John Smith) so I want to pick one of the next three where "assignedToUser" : null and save the variables in order to build up the URL to be able to open that course approval.  For the second approval in the json above the URL would be https://www.example..../11/approval/20 So I need to save the courseID, the applicationID and the approvalID.

 

My primary problems seem to be

1) the json is structured so that I need to drill up from the thing I want to match on

2) there are nested {  } so that I can't use those as the search parameter to grab the element I want

3) there are three variables called 'ID' so a) I can't use that to grab the element I want and B) I need to differentiate between them based on where they are in the json 

4) I tried using the jpath parameter and either I'm using it wrongly or the json isn't the right format because even a simple test like $jPath(/Element[2],NO) returns Error : cannot find the specified value in the server response

 

I'm sure there is a really simple answer to this but I'm a bit stumped.  Please can you point me in the right direction?

 

Thanks,
Trish



#2 Sergei Leonov

Sergei Leonov

    Advanced Member

  • Administrators
  • PipPipPip
  • 662 posts

Posted 28 July 2017 - 10:46 AM

You need to use the $JPath function. It is not trivial but possible.

 

XPath //*[assignedToUser/@type='null'] return you all three elements with "assignedToUser" : null.

 

XPath //*[assignedToUser/@type='null'][1] return you the first element with "assignedToUser" : null

 

So for courseID you can use $JPath(//*[assignedToUser/@type='null'][1]/application/course/id,NO).

For applicationID you can use $JPath(//*[assignedToUser/@type='null'][1]/application/id,NO).

For approvalID you can use $JPath(//*[assignedToUser/@type='null'][1]/id,NO).

 

If it will not work for you, perhaps, you need to upgrade to the latest version of WAPT to use that.


Sergei Leonov
WAPT Project Manager
SoftLogica LLC

Office: +7-383-335-6692
Toll-free: 1-888-364-6797 (for US only)
Time zone GMT +6, EST +11
E-mail: support@loadtestingtool.com
http://www.loadtestingtool.com





Also tagged with one or more of these keywords: json

1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users