Using JSONPath Syntax#
Introduction#
The syntax and examples with slight changes are taken from the jsonpath Github and are adapted from Stefan Goessner’s original post.
JSONPath |
Description |
---|---|
|
The root object/element |
|
The current object/element |
|
Child member operator |
|
Recursive descendant operator; JSONPath borrows this syntax from E4X |
|
Wildcard matching all objects/elements regardless their names |
|
Subscript operator |
|
Union operator for alternate names or array indices as a set |
|
Array slice operator borrowed from ES4 / Python |
|
Applies a filter (script) expression via static evaluation |
|
Script expression via static evaluation |
Given this sample data set, see example expressions below:
{
"store": {
"book": [
{
"category": "reference",
"author": "Nigel Rees",
"title": "Sayings of the Century",
"price": 8.95,
"tags": ["book", "Rees", "reference", "Sayings"]
},
{
"category": "fiction",
"author": "Evelyn Waugh",
"title": "Sword of Honour",
"price": 12.99,
"tags": ["book", "Waugh", "fiction", "Sword"]
},
{
"category": "fiction",
"author": "Herman Melville",
"title": "Moby Dick",
"isbn": "0-553-21311-3",
"price": 8.99,
"tags": ["book", "Melville", "fiction", "Moby"]
},
{
"category": "fiction",
"author": "J. R. R. Tolkien",
"title": "The Lord of the Rings",
"isbn": "0-395-19395-8",
"price": 22.99,
"tags": []
}
],
"bicycle": {
"color": "red",
"price": 19.95
}
}
}
Example JSONPath expressions:
JSONPath |
Description |
---|---|
|
The authors of all books in the store |
|
All authors |
|
All things in store, which are some books and a red bicycle |
|
The price of everything in the store |
|
The third book |
|
The last book via script subscript |
|
The last book via slice |
|
The first two books via subscript union |
|
The first two books via subscript array slice |
|
Filter all books with isbn number |
|
Filter all books cheaper than 10 |
|
Filter all books that cost 8.95 |
|
Filter all fiction books cheaper than 30 |
|
All members of JSON structure |
To easily test those examples you can use the jsonPath online evaluator.