Here are some use cases to use the JSON Path with the JSON API Source
Definition
A JSONPath expression begins with the dollar sign ($), which represents the root element. An expression is formed by following this up with a sequence of child elements, separated by a period (.), called the dot notation or square brackets (['...']) called the bracket notation.
Suppose, you want to extract the agent name from the following JSON document:
{
"event": {
"agency": "MI6",
"data": {
"name": "James Bond",
"id": "007"
}
}
}
In this example, the root node is event
, and the child nodes are agency
and data
. The agent name is found in the child element, name
, of the node, data
.
Thus, the JSONPath expression to extract the agent name is:
In dot-notation:
$.event.data.name
In bracket-notation:
$['event']['data']['name']
The response returns a string that contains the agent name.
"James Bond"
JSONPath Expressions
Expression | Description |
| The root node or element. |
| Wildcard. Selects all elements in an object or array. |
| A recursive scan. Selects and returns a list of all the properties of the element that matches
|
| The dot-notated child element of the current node. |
| The bracket-notated child or children of the current node. |
| The n-th element of an array. Indices start from 0. |
| The array elements specified by the given indices. The result is a list of elements. |
| The array elements from the start index up to, but not including, the end index. If end is omitted, selects all elements from start until the end of the array. The result is a list of elements. |
| The first n elements of the array. The result is a list of elements. |
| The last n elements of the array. The result is a list of elements. |
| A filter expression. The expression must return a boolean value. The result is a list of all the elements in an object or array that match the specified filter. |
| The current node or element being processed in the filter expression. |
Filters
Filters are logical expressions that you can use to sieve information from arrays. A filter expression is enclosed in square brackets and returns a boolean value.
The following is an example of a JSONPath expression with a filter:
$.person.[?(@.age > 18)]
where @
represents the current array item or object being processed. The expression returns a list of people who are above 18 years of age.
You must enclose string values within single or double quotes. For example, [?(@.color == 'blue')]
or [?(@.color == "blue")]
.
You can create more complex filters with the logical operators &&
and ||
.
Operator | Description |
| Equal to. |
| Not equal to. |
| Less than. |
| Less than or equal to. |
| Greater than. |
| Greater than. |
| Matches a regular expression. |
| Negates a filter. |
| Logical AND. Used to combine multiple filter expressions where all the conditions must be met. |
| Logical OR. Used to combine multiple filter expressions where any of the conditions may be met. |
| Checks if the value on the left exists in the set on the right. |
| Checks if the value on the left is not in the set on the right. |
| Checks if the size of the array or the string matches the specified value. |
| Matches an empty array or string. |
| Matches a non-empty array or string. |
JSONPath Expression Examples
Let us use the following JSON document to form sample JSONPath expressions:
{ "event":
{ "name": "Bond Movies",
"movies":
[
{
"name": "Licence to Kill",
"star": "Timothy Dalton",
"rating": 6.6
},
{
"name": "GoldenEye",
"star": "Pierce Brosnan",
"rating": 7.2
},
{
"name": "Tomorrow Never Dies",
"star": "Pierce Brosnan",
"rating": 6.5
},
{
"name": "Skyfall",
"star": "Daniel Craig",
"rating": 7.8
}
]
}
}
You can query for the details of each movie in the document using the following JSONPath expression:
$.event.movies
The response is a list of all the movies from the document.
Similarly, you can filter the movies with a rating greater than 7 using the following expression:
$.event.movies[?(@.rating > 7)]
The response returns only those movies whose rating is more than 7.
You can form more queries, based on the level of granularity you need, using the expressions in the following table:
Note: This is not a comprehensive list. You can form expressions to suit your requirement using the supported JSONPath syntax and filters.
JSONPath Expression | Meaning |
| Entire object. |
| Event object. |
| First movie. |
| First movie. |
| First three movies. |
| First two movies. |
| Last two movies. |
| All movies with a rating greater than 7. |
| All movies whose star is Pierce Brosnan. |