JSON Path
Similarly to the widely used XPath language used to select particular part of a xml structure, the JSON Path can be used to select a particular part of a json structure.
Introduction
Though no consensus seems yet to exists around the JSONPath standard and rules, some interesting specification can be found at https://www.ietf.org/archive/id/draft-goessner-dispatch-jsonpath-00.html
Basics
Some concepts of JSON Path language
$
Root element of the target data.
.
Separator in the selection path.
[]
Wrapper of the expression used to filter the data
@
Current element into a data filtering expression
Example
Some examples from the previously mentioned specification page
Target JSON
{
"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
}
}
}Filtering expressions
/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 data item.
Testing
JSONPath testing is available at : https://jsonpath.com
Last updated
Was this helpful?