|
|
JSONPathParser Function |
| Number of input field | : | One |
| Number of output field | : | One |
JSONPath uses the symbol $ to represent the root (outer-level) object.
JSONPath expressions can use the dot notation, for example:
$.store.book[0].title
or square bracket notation, for example:
$['store']['book'][0]['title']
JSONPath allows the wildcard symbol * for member names and array indices.
Expressions (<expr>) can be used as an alternative to names or indices, for example:
$.store.book[(@.length-1)].title
In this case, the symbol '@' refers to the current object.
Filter expressions are supported using the syntax ?(<boolean expr>), for example :
$.store.book[?(@.price < 10)].title
| XPath | JSONPath | Description |
| / | $ | the root object/element |
| . | @ | the current object/element |
| / | . or [] | child operator |
| .. | n/a | parent operator |
| // | .. | recursive descent. |
| * | * | wildcard. All objects/elements regardless their names. |
| @ | n/a | attribute access. JSON structures don't have attributes. |
| [] | [] | subscript operator |
| | | [,] | Union operator |
| n/a | [start:end:step] | array slice operator . |
| [] | ?() | applies a filter (script) expression.. |
| n/a | () | script expression, using the underlying script engine. |
| () | n/a | grouping in Xpath |
Below is a simple JSON structure example representing a bookstore.
{ "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 and JSONPath sample expressions and their results.
| 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 are 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 in order. |
//book[position()<3] | $..book[0,1]$..book[:2] | the first two 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 JSON structure. |
$..book[?(@.price<{1})]
| JSONPath Expresssion | $.store.book |
| Result | {{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}} |
The result of the JSONPath expression is an array of book objects, where each book is represented as:
{ "category": "value1", "author": "value2", ... }
To process this array:
| JSONPath Expresssion | $.store.book[?(@.price < 10)] |
| Result | {{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}} |
| JSONPath Expresssion | $.store.book[*].title |
| Result | {Sayings of the Century,Sword of Honour,Moby Dick,The Lord of the Rings} |
| JSONPath Expresssion | $.store.book[?(@.price < 10)].title |
| Result | {Sayings of the Century,Moby Dick} |
| JSONPath Expresssion | $.store.book[0].title |
| Result | Sayings of the Century |
| JSONPath Expresssion | $.store |
| Result | {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}} |
Notes: field and value separator for every level
| JSONPath Expresssion | $..book[?(@.isbn)] |
| Result | {{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}} |
| JSONPath Expresssion | $..book[?(!@.isbn)] |
| Result | {{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}} |
| JSONPath Expresssion | $..book[?(@.category=='reference')] |
| Result | {{category:reference,author:Nigel Rees,title:Sayings of the Century,price:8.95}} |