JSONPath Syntax - AlertSite Documentation
JSONPath Syntax - AlertSite Documentation
! AlertSite Documentation
AlertSite Documentation / Monitors / API Monitoring / API Endpoint Monitors
" Introducing AlertSite
!
Tutorials
! API Monitoring JSONPath is a query language for JSON, similar to XPath for XML. AlertSite API endpoint monitors
let you use JSONPath in assertions to specify the JSON !elds that need to be veri!ed. "
$ ! API Endpoint Monitors
!
!
Create From OpenAPI (Swagger) JSONPath notation
&
!
!
Assertions
A JSONPath expression speci!es a path to an element (or a set of elements) in a JSON structure.
!
Variables Paths can use the dot notation:
!
Monitor Settings
$.[Link][0].title
!
Convert to SoapUI
or the bracket notation:
!
JSONPath Syntax
' " Con!guring Monitors Tip: Use this notation if the property name contains special
characters such as spaces, or begins with a character other than
" Dashboards
( A..Za..z_ .
" Alerts
[n] Selects the n-th element from an array. Indexes are 0-based.
[index1,index2,…] Selects array elements with the speci!ed indexes. Returns a list.
[start:end] Selects array elements from the start index and up to, but not
[start:] including, end index. If end is omitted, selects all elements from
start until the end of the array. Returns a list.
Notes:
Unlike XPath, JSONPath does not have operations for accessing parent or sibling nodes
from the given node.
Filters
Filters are logical expressions used to !lter arrays. An example of a JSONPath expression with a
!lter is
where @ represents the current array item or object being processed. Filters can also use $ to
refer to the properties outside of the current object:
An expression that speci!es just a property name, such as [?(@.isbn)] , matches all items that
have this property, regardless of the value.
Operator Description
== Equals to. String values must be enclosed in single quotes (not double
quotes): [?(@.color=='red')] .
! Used to negate a !lter: [?(!@.isbn)] matches items that do not have the
isbn property.
in Checks if the left-side value is present in the right-side list. Similar to the SQL
IN operator. String comparison is case-sensitive.
[?('S' in @.sizes)]
nin Opposite of in . Checks that the left-side value is not present in the right-side
list. String comparison is case-sensitive.
subsetof Checks if the left-side array is a subset of the right-side array. The actual
order of array items does not matter. String comparison is case-sensitive. An
empty left-side array always matches.
For example:
Examples
For these examples, we will use a modi!ed version of JSON from
[Link]
{
"store": {
"book": [
{
"category": "reference",
"author": "Nigel Rees",
"title": "Sayings of the Century",
"price": 8.95
},
{
"category": "fiction",
"author": "Herman Melville",
"title": "Moby Dick",
"isbn": "0-553-21311-3",
"price": 8.99
},
{
"category": "fiction",
"author": "J.R.R. Tolkien",
"title": "The Lord of the Rings",
"isbn": "0-395-19395-8",
"price": 22.99
}
],
"bicycle": {
"color": "red",
"price": 19.95
}
},
"expensive": 10
}
Expression Meaning
$.store.* All direct properties of store (not recursive).
Result: red
Result:
[Sayings of the Century,
Moby Dick,
The Lord of the Rings]
Result:
[
{
"category":"reference",
"author":"Nigel Rees",
"title":"Sayings of the
Century",
"price":8.95
}
]
{
"name": "Rose Kolodny",
"phoneNumbers": [
{
"type": "home",
"number": "954-555-1234"
},
{
"type": "work",
"number": "754-555-5678"
}
]
}
phoneNumbers[*].number
[954-555-1234, 754-555-5678]
Note that this is not a JSON array, it is just a comma-separated list of items where [ ] indicates
the beginning and end of the list.
When using “equals” assertions against a list of matches, specify a list of expected values
enclosed in [ ] and separated by a comma and one space:
Standalone strings (like apples ) should not have enclosing quotes, unless the quotes are part of
the value.
Example
Values that are JSON arrays and objects keep inner quotes, but are mini!ed with no spaces
between their items: ["foo","bar"] , not [ "foo" , "bar" ] .
FAQ
How can I check that my JSONPath syntax is valid?
If you have ReadyAPI 1.9, you can create a test for your API endpoint, add a JSONPath Match
assertion and test the syntax in the assertion editor there.
Otherwise, you can use [Link] and check the results on the Jayway tab.
However, the syntax used on this site may be slightly di"erent from the one used in AlertSite.
See Also
API Assertions
Creating an API Endpoint Monitor
API Endpoint Monitor Settings
CrossBrowserTesting Zephyr
!
!