The simplest definition would be "JSONPath is to JSON as XPath is to XML".
Please check the following documentation if you are not familiar with JSON - JSON Guide
Following are two of the main advantages of using JSONPath:
- Data may be interactively found and extracted out of JSON structures on the client without special scripting.
- JSON data requested by the client can be reduced to the relevant parts on the server, such minimizing the bandwidth usage of the server response.
A JSONPath expression contains the following symbols:
$– symbol refers to the root object or element.
@– symbol refers to the current object or element.
.– operator is the dot-child operator, which you use to denote a child element of the current element.
[ ]– is the subscript operator, which you use to denote a child element of the current element (by name or index).
*– operator is a wildcard, returning all objects or elements regardless of their names.
,– operator is the union operator, which returns the union of the children or indexes indicated.
:– operator is the array slice operator, so you can slice collections using the syntax
[start:end:step]to return a subcollection of a collection.
( )– operator lets you pass a script expression in the underlying implementation’s script language. It’s not supported by every implementation of JSONPath, however.
? ( )– to query all items that meet a certain criteria.
Given the json:
|$.store.book[*].author||The authors of all books|
|$.store.*||All things, both books and bicycles|
|$.store..price||The price of everything|
|$..book||The third book|
|$..book[-2]||The second to last book|
|$..book[0,1]||The first two books|
|$..book[:2]||All books from index 0 (inclusive) until index 2 (exclusive)|
|$..book[1:2]||All books from index 1 (inclusive) until index 2 (exclusive)|
|$..book[-2:]||Last two books|
|$..book[2:]||Book number two from tail|
|$..book[?(@.isbn)]||All books with an ISBN number|
|$.store.book[?(@.price < 10)]||All books in store cheaper than 10|
|$..book[?(@.price <= $['expensive'])]||All books in store that are not "expensive"|
|$..book[?(@.author =~ /.*REES/i)]||All books matching regex (ignore case)|
|$..*||Give me every thing|
|$..book.length()||The number of books|
More details can be found here:
Java port for JSONPath
And here's an online JSONPath evaluator: JSONPath Online evaluator
Happy Test Automation!