Overview
JSONPath expressions can be used to parse JSON-structured data. For details, see the official API document: https://github.com/json-path/JsonPath.
Basic Operator
The root element in JSONPath is always referred to as $ regardless if it is an object or array.
JSONPath expressions allow you to use the dot notation like $.store.book[0].title or the square bracket notation like $['store']['book'][0]['title'].
Operator | Description |
---|---|
$ | It is the root element. |
. or [] | To obtain the child element. Example: store.book or store["book"] |
.. | It is the descendant selector for selecting all elements that meet the condition. |
[,] | It is the coalescing operator for selecting multiple elements. Example: book [0,1] or $.. book [0].["category,""author"] |
[start:end] | It is the slicing operator, with the index starting from 0, and a left-closed, right-open range. Example: book [0:2] or book [-2:0] |
?() | It is the filter identifier for selecting elements in an array, where the filtering condition can be written within parentheses. |
@ | It represents the element currently being processed by the filter, usually used with the filter. Example: book [?(@. price <10)] |
* | It is the wildcard for matching the element name or index. Example: book.* or book[*] It can also be used with the filter as part of a regular expression. Example: book[?(@.author =~ /.*ees/)] |
Filter Operator
Filters are logical expressions used to filter arrays. A typical filter is [?(@.age > 18)] where @ represents the current element being processed. You can create more complex filters with logical operators && and ||. The value of the string must be enclosed by single or double quotes, such as ([?(@.color == 'blue')] or [?(@.color == "blue")]).
Operator | Description |
---|---|
== | Left is equal to right.![]() |
!= | Left is not equal to right. |
< | Left is less than right. |
<= | Left is less than or equal to right. |
> | Left is greater than right. |
>= | Left is greater than or equal to right. |
=~ | Left matches the regular expression of right. Example: [?(@. name =~/foo. *?/i)] |
in | Left exists in right. Example: [?(@.size in ['S', 'M'])] |
nin | Left does not exists in right. |
subsetof | Left is a subset of right. Example: [?(@. sizes subsetof ['S', 'M', 'L'])] |
anyof | Left has an intersection with right. Example: [?(@.sizes anyof ['M', 'L'])] |
noneof | Left has no intersection with right. Example: [?(@.sizes noneof ['M', 'L'])] |
size | Size of left (array/string) should match right. |
empty | Left (array/string) should be empty. |
Function
You can invoke a function at the end of a path.
Function | Description | Output Type |
---|---|---|
max() | Provides the maximum value of an array of numbers. | Double precision |
avg() | Provides the average value of an array of numbers. | Double precision |
stddev() | Provides the standard deviation value of an array of numbers. | Double precision |
length() | Provides the length of an array. | Integer |
sum() | Provides the sum value of an array of numbers. | Double precision |
Example:
Here is a JSON structure.
{
"store": {
"book": [
{
"category": "reference",
"author": "Nigel Rees",
"title": "Sayings of the Century",
"price": 8.95
},
{
"category": "fiction",
"author": "Evelyn Waugh",
"title": "Sword of Honour",
"price": 12.99
},
{
"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
}
JSONPath Expression | JSONPath Expression |
---|---|
$.store.book[*].author | The authors of all books in the store element |
$..author | All authors in the root element |
$.store.* | Everything in the store element, whether it is a book or a bicycle |
$.store..price | The price of everything in the store element |
$..book[2] | The third book |
$..book[-2] | The second to last book |
$..book[0,1] $..book[:2] | The first two books |
$..book[:2] | The first two books (Note: The index of the first book is 0.) |
$..book[1:2] | The second book |
$..book[-2:] | Last two books |
$..book[2:] | The third to the last book |
$..book[?(@.isbn)] | All books with an ISBN number |
$.store.book[?(@.price < 10)] | All books cheaper than 10 |
$..book[?(@.price <= $['expensive'])] | All books where the price does not exceed the value of expensive (The value of expensive is 10.) |
$..book[?(@.author =~ /.*REES/i)] | All books matching the regular expression Meaning of the regular expression: The author's name ends with REES, which is not case-sensitive. |
$..* | Everything in the root element |
$.store.book.length() | The total number of books |