Core Step verifyJsonPath
Description
This step verifies that an JsonPath expression is true or has a certain value.
More documentation for JsonPath, including descriptions of operators and functions available can be found at the Jway JsonPath Github site, and at Stefan Goessner's orginal article.
The Jayway JsonPath Evaluator, is a good online resource for testing out JsonPath expressions on your data.
Parameters
- description
- Required? no
- The description of this test step.
- jpath
- Required? true
- The JsonPath expression to evaluate.
- text
- Required? no
- The expected value of the JSON path evaluation. If omitted the step checks that the JsonPath exists and not empty.
- tolerance
- Required? no
- The maximum difference allowed between two floating point values in a response. This value, if set, must be greater than 0.0, otherwise it is ignored. Default value = 0.01.
Details
JsonPath expressions always refer to a JSON structure in the same way as XPath expression are used in combination with an XML document. The "root member object" in JsonPath is always referred to as $ whether it is an object or array.
JsonPath expressions can use the dot–notation:
or the bracket–notation:
Operators
- $
- The root element to query. This starts all path expressions.
- @
- The current node being processed by a filter predicate.
- *
- Wildcard. Available anywhere a name or numeric are required.
- ..
- Deep scan. Available anywhere a name is required.
- .<name>
- Child reference using dot notation.
- ['<name>' (, '<name>')]
- Child reference using bracket notation. Multiple children can be listed in the brackets.
- [<number> (, <number>)]
- Array index (or multiple idexes).
- [start:end]
- Array slice operator.
- [?(<expression>)] Filter expression.
- Expression must evaluate to a boolean value.
What gets returned from JsonPath?
When evaluating a path you need to understand the concept of when a path is indefinite. A path is indefinite if it contains:
- ..
- A deep scan operator
- ?(<expression>)
- An expression
- [<number>, <number> (, <number>)]
- multiple array indexes
Simple usage examples:
<config ... />
<invoke ... />
<-- given a response like: {"msg": "hello"} -->
<verifyJsonPath
description="Check hello"
jpath="$.msg"
text="hello" />
<-- check floating point response: {"pi": 3.14159} -->
<verifyJsonPath
description="Check PI"
jpath="$.pi"
text="3.14" />
</webtest>
The following examples extract a variety of subsets from the following JSON.
{
"sun" : {
"aRA" : 21.5376,
"aD" : -14.599,
"tRA" : 21.53750,
"tD" : -14.600645,
"tAlt" : -4.25582,
"azimuth" : 255.58,
"illuminance" : 23.6143
},
"moon" : {
"aRA" : 22.70083,
"aD" : -6.99388,
"tRA" : 22.6461,
"tD" : -7.52695,
"tAlt" : 13.4022,
"azimuth" : 253.00,
"illuminance" : 0.000024,
"percentIllumination" : " 3%+"
},
"total" : {
"illuminance" : 23.61486
},
"dateTime" : "2016-02-10T00:00:00",
"latitude" : 30.0,
"longitude" : -90.0,
"skyCondition" : "VISIBLE"
},
{
"sun" : {
"aRA" : 21.5706,
"aD" : -14.4379,
"tRA" : 21.5708,
"tD" : -14.4391,
"tAlt" : -10.1747,
"azimuth" : 100.89,
"illuminance" : 0.038165
},
"moon" : {
"aRA" : 23.1694,
"aD" : -4.81467,
"tRA" : 23.2205,
"tD" : -5.27338,
"tAlt" : -27.05082,
"azimuth" : 79.88,
"illuminance" : 4.23209E-25,
"percentIllumination" : " 5%+"
},
"total" : {
"illuminance" : 0.038665
},
"dateTime" : "2016-02-10T12:00:00",
"latitude" : 30.0,
"longitude" : -90.0,
"skyCondition" : "VISIBLE"
}
]
<config ... />
<invoke ... an endpoint that returns the example JSON. ... />
<verifyJsonPath
description="Check all illumination values"
jpath="$..illuminance"
tolerance="0.001"
text="[ 23.614, 2.4E-5, 23.615, 0.038165, 0.0, 0.0387 ]" />
<verifyJsonPath
description="Check the sun azimuth @ 12:00"
jpath="$..[?(@.dateTime == '2016-02-10T12:00:00')].sun.azimuth"
text="100.89" />
<verifyJsonPath
description="Check the total object @ 12:00"
jpath="$..[?(@.dateTime == '2016-02-10T12:00:00')].total"
tolerance="0.001"
text="{ "illuminance" : 0.038665 }" />
<verifyJsonPath
description="Check both sun and moon alitude and azimuth @ T00"
jpath="$..[?(@.dateTime =~ /.*T00.*?/i)].['sun','moon'].['tAlt','azimuth']"
text="[ { "tAlt" : -4.25582, "azimuth" : 255.58 }, { "tAlt" : 13.4022, "azimuth" : 253.0 } ] " />
</webtest>
See also: the storeJsonPath step.