Jump to content
WAPT Forum
Sign in to follow this  
trish.mcdonough

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

Recommended Posts

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.com/#/courses/1000010/applications/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

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...