Evaluate JSON Path Expression Assertion
Evaluate JSON Path Expression assertion is used to query JSON objects, similar to querying XPaths. You enter a JSON expression and this assertion will parse the target message and place the results in context variables.
gateway83
Evaluate JSON Path Expression
assertion is used to query JSON objects, similar to querying XPaths. You enter a JSON expression and this assertion will parse the target message and place the results in context variables.Place a Protect Against JSON Document Structure Threats Assertion before this assertion to protect against DOS attacks.
Detecting Invalid JSON
The Evaluate JSON Path Expression assertion fails if the expression does not match. But it also fails when an invalid JSON payload is encountered and this may not be evident. One solution is to use this assertion with the expression of '$'. This selects the root node and validates the JSON payload. If it is invalid, the expression will fail.
Using Strings
If you need to use string values with the Evaluate JSON Path Expression assertion, convert them to message format first. To do this, convert the String to a Message using the Set Context Variable Assertion, with "Data type: Message" and "Content-Type:application/json".
Context Variables Created by This Assertion
The Evaluate JSON Path Expression assertion sets the following context variables. The default
<prefix>
is "jsonPath" and can be changed in the assertion properties.To learn about selecting the target message for this assertion, see Selecting a Target Message.
Variable | Description |
< prefix >.found | Indicates whether a match was found for the expression:
|
< prefix >.count | Returns the number of matches. |
< prefix >.result | Returns the result of the match, if a single match was made. |
< prefix >.results | Returns the results of the match, if multiple matches were made. |
Using the Assertion
- Do one of the following:
- To add the assertion to the Policy Development window, see Add an Assertion.
- To change the configuration of an existing assertion, proceed to step 2 below.
- When adding the optional assertion, the Evaluate JSON Path Expression Properties automatically appear; when modifying the assertion, right-clickin the policy window and select<target>:Evaluate JSON Path ExpressionEvaluate JSON Path Expression Propertiesor double-click the assertion in the policy window. The assertion properties are displayed.
- Configure the optional evaluator and expression.SettingDescriptionEvaluatorCurrently, the only supported evaluator isJsonPath.ExpressionEnter the expression to be matched against a message. You may reference context variables.The Expression field can only contain a single expression. To evaluate multiple expressions, configure multiple Evaluate JSON Expression assertions within a policy
- Configure[Source and Destination]tab as follows:SettingDescriptionTarget MessageSpecify whether to match against theRequest,Response, orOther Message Variablethat contains the value to analyze. If other variable, specify the variable name is the box. (You do not need to enclose the variable name within the "${ }" characters.)The message target can also be set outside of the assertion properties. For more information, see Selecting a Target Message.Variable PrefixEnter a prefix that will be added to the context variables created by this assertion. This prefix will ensure uniqueness and will prevent the variables from overwriting each other when multiple instances of this assertion appear in a policy.For an explanation of the validation messages displayed, see Context Variable Validation.
- Select the [Test] tab to test your JSON expression against sample test input.
- In theTest Inputbox, paste some JSON code that might be found in the target message. When you click the [Test] button, the assertion attempts to the specified expression against the test input.
- TheTest Outputbox shows the results of the match. Examine the results carefully to see if this is what you intended. The figure below illustrates how the assertion interprets the test input given the sample expression shown.
- For the test input shown in the figure, the test results arefound = trueandcount = 2.This means that there were two inputs that fulfilled the criteria:Nigel ReesandEvelyn Waugh. The assertion was able to locate the author but not the category, title, or price, because they were not specified.
- If you used the test input"bicycle", no test results will appear because it does not fulfill the expression criteria ".author".
- Click [OK] when done.