What is JSONPath


JSONPath is to JSON what XPath is to XML. Just as XPath allows us to traverse and fetch information from the XML Document, JSONPath expression allows us to do so for JSON documents. Due to the popularity of JavaScript, JSON is one of the most widely used data storage formats on the web and this, in turn, increases the utility of JSONPath.


Pre-requisite:

You should be familiar with JSON.


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 as minimizing the bandwidth usage of the server response.

Syntax:

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 certain criteria.

Path Examples:

Given the 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
        }
    },
    "expensive": 10
}
JSONPath Result
$.store.book[*].author The authors of all books
$..author All authors
$.store.* All things, both books and bicycles
$.store..price The price of everything
$..book[2] 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

A simple guide on JSONPath from Restfulapi.net

And here's an online JSONPath evaluator: JSONPath Online evaluator