Introduction

The simplest definition would be "JSONPath is to JSON as XPath is to XML".


Please check the following documentation if you are not familiar with JSON - JSON Guide


As XPath allows us to traverse and fetch information from the XML Document, JSONPath expression allows us to do the same for JSON Documents. Due to the huge popularity of JavaScript language, JSON is one of the most widely used Data Storage format on the Web and this in turn increases the utility of JSONPath.


Following are two of the 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 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 a 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
}


JSONPathResult
$.store.book[*].authorThe authors of all books
$..authorAll authors
$.store.*All things, both books and bicycles
$.store..priceThe 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



Happy Test Automation!