Xpath |
JSONPath |
Description |
/ |
$ |
The root object/element |
. |
@ |
The current object/element |
/ |
. or [] |
Child operator |
.. |
N/A |
Parent operator |
// |
.. |
Recursive descent. JSONPath borrows this syntax from E4X. |
* |
* |
Wildcard. All objects/elements regardless their names. |
@ |
N/A |
Attribute access. JSON structures don ' t has attributes. |
[] |
[] |
Subscript operator. XPath uses it to iterate over element collections and for predicates. In Javascript and JSON it is the native array operator. |
| |
[,] |
Union operator in XPath results in a combination of node sets. JSONPath allows alternate names or array indices as a set. |
N/A |
[Start:end:step] |
Array slice operator borrowed from ES4. |
[] |
? () |
Applies a filter (script) expression. |
N/A |
() |
Script expression, using the underlying script engine. |
() |
N/A |
Grouping in Xpath |
JSONPath expressions can use the dot–notation
$.store.book[0].title
or the bracket–notation
$[‘store‘][‘book‘][0][‘title‘]
The following XPath expression
/store/book[1]/title
JsonPath would look like
x.store.book[0].title
Or
x[‘store‘][‘book‘][0][‘title‘]
JSONPath Examples
123456789Ten One A - - the - - - + - + A at - - - - - in - to |
{ "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 } } } |
Xpath |
JSONPath |
Result |
/store/book/author |
$.store.book[*].author |
The authors of all books in the store |
//author |
$..author |
All authors |
/store/* |
$.store.* |
All things in store, which is some books and a red bicycle. |
/store//price |
$.store..price |
The price of everything in the store. |
//book[3] |
$..book[2] |
The third book |
//book[last()] |
$..book[(@.length-1)]
$..book[-1:] |
The last book is in order. |
//book[position()<3] |
$..book[0,1]
$..book[:2] |
The first and the books |
//book[isbn] |
$..book[?(@.isbn)] |
Filter All books with ISBN number |
//book[price<10] |
$..book[?(@.price<10)] |
Filter all books Cheapier than 10 |
//* |
$..* |
All Elements in XML document. All members of the JSON structure. |
Required JAR Package
Json-path-0.9.1.jar
Json-smart-1.2.jar
Commons-lang-2.6.jar
JsonPath syntax vs. XPath