JSON JSONPath
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 fields that need to be verified.
JSONPath Notation
A JSONPath expression specifies a path to an element (or a set of elements) in a JSON structure. Paths can use the dot notation:
$.store.book[0].title
or the bracket notation:
$['store']['book'][0]['title']
The leading $
represents the root object or array and can be omitted. For example, $.foo.bar
and foo.bar
are the same, and so are $[0].status
and [0].status
.
Other syntax elements are described below.
Expression | Description |
---|---|
$ |
The root object or array. |
.property |
Selects the specified property in a parent object. |
['property'] |
Selects the specified property in a parent object. Be sure to put single quotes around the property name. Tip: Use this notation if the property name contains special characters such as spaces, or begins with a character other than |
[n] |
Selects the n-th element from an array. Indexes are 0-based. |
[index1,index2,…] |
Selects array elements with the specified indexes. Returns a list. |
..property |
Recursive descent: Searches for the specified property name recursively and returns an array of all values with this property name. Always returns a list, even if just one property is found. |
* |
Wildcard selects all elements in an object or an array, regardless of their names or indexes. For example, |
[start:end] [start:] |
Selects array elements from the start index and up to, but not including, end index. If end is omitted, selects all elements from start until the end of the array. Returns a list. |
[:n] |
Selects the first n elements of the array. Returns a list. |
[-n:] |
Selects the last n elements of the array. Returns a list. |
[?(expression)] |
Filter expression. Selects all elements in an object or array that match the specified filter. Returns a list. |
[(expression)] |
Script expressions can be used instead of explicit property names or indexes. An example is [(@.length-1)] which selects the last item in an array. Here, length refers to the length of the current array rather than a JSON field named length . |
@ |
Used in filter expressions to refer to the current node being processed. |
Notes:
-
JSONPath expressions, including property names and values, are case-sensitive.
-
Unlike XPath, JSONPath does not have operations for accessing parent or sibling nodes from the given node.
Filters
Filters are logical expressions used to filter arrays. An example of a JSONPath expression with a filter is
$.store.book[?(@.price < 10)]
where @
represents the current array item or object being processed. Filters can also use $
to refer to the properties outside of the current object:
$.store.book[?(@.price < $.expensive)]
An expression that specifies just a property name, such as [?(@.isbn)]
, matches all items that have this property, regardless of the value.
Additionally, filters support the following operators:
Operator | Description |
---|---|
== |
Equals to. 1 and '1' are considered equal. String values must be enclosed in single quotes (not double quotes): [?(@.color=='red')] . |
!= |
Not equal to. String values must be enclosed in single quotes. |
> |
Greater than. |
>= |
Greater than or equal to. |
< |
Less than. |
<= |
Less than or equal to. |
=~ |
Match a JavaScript regular expression. For example, Note: Not supported at locations that use Ready! API 1.1. |
! |
Use to negate a filter: Note: Not supported at locations that use Ready! API 1.1. |
&& |
Logical AND, used to combine multiple filter expressions:
[?(@.category=='fiction' && @.price < 10)] |
|| |
Logical OR, used to combine multiple filter expressions: [?(@.category=='fiction' || @.price < 10)] Note: Not supported at locations that use Ready! API 1.1. |
Examples
For these examples, we will use a modified version of JSON from http://goessner.net/articles/JsonPath/index.html#e3:
{
"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
}
In all these examples, the leading $.
is optional and can be omitted.
Expression | Meaning |
---|---|
$.store.* |
All direct properties of store (not recursive). |
$.store.bicycle.color |
The color of the bicycle in the store. Result: |
$.store..price $..price |
The prices of all items in the store. Result: |
$.store.book[*] $..book[*] |
All books in the store. |
$..book[*].title |
The titles of all books in the store. Result: |
$..book[0] |
The first book. Result: |
$..book[0].title |
The title of the first book. Result: |
$..book[0,1].title $..book[:2].title |
The titles of the first two books. Result: |
$..book[-1:].title $..book[(@.length-1)].title |
The title of the last book. Result: The result is a list, because |
$..book[?(@.author=='J.R.R. Tolkien')].title |
The titles of all books by J.R.R. Tolkien (exact match, case-sensitive). Result: The result is a list, because filters always return lists. |
$..book[?(@.isbn)] |
All books that have the isbn property. |
$..book[?(!@.isbn)] |
All books without the isbn property. |
$..book[?(@.price < 10)] |
All books cheaper than 10. |
$..book[?(@.price > $.expensive)] |
All expensive books. |
$..book[?(@.author =~ /.*Tolkien/i)] |
All books whose author name ends with Tolkien (case-insensitive). |
$..book[?(@.category == 'fiction' || @.category == 'reference')] |
All fiction and reference books. |
$..* |
All members of the JSON structure beneath the root (child objects, individual property values, array items), combined into an array. |
Considerations for JSONPath Expressions That Return Multiple Elements
JSONPath queries can return not just a single element, but also a list of matching elements. For example, given this JSON:
{
"name": "Rose Kolodny",
"phoneNumbers": [
{
"type": "home",
"number": "954-555-1234"
},
{
"type": "work",
"number": "754-555-5678"
}
]
}
the JSONPath expression
phoneNumbers[*].number
returns a list containing two phone numbers:
[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:
[apples, 15, false, ["foo","bar"], {"status":"ok"}]
Standalone strings (like apples
) should not have enclosing quotes, unless the quotes are part of the value.
Values that are JSON arrays and objects keep inner quotes, but are minified 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 Ready! API 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 http://jsonpath.herokuapp.com and check the results on the Jayway tab. However, the syntax used on this site may be slightly different from the one used in AlertSite.
See Also
API Assertions
Creating an API Endpoint Monitor
API Endpoint Monitor Settings
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构