API: Working with Jayway JSONPath expression

While working with extracting data from JSON , we feel the need of expression which helps us to fetch the data from a complex JSON with conditions.

Based on the conditions and logic we provide, it will return the data if the condition matches.

It is indeed very helpful and must have feature in our API testing.
Jayway jsonpath is the best solution this.

Head on to this beautiful website which helps you filter JSON using JsonPath expression:

For more information about the library that we will use here is below:
Check the instructions on how to implement in your java project.

About the expression:
JSONPath expressions always refer to a JSON structure in the same way as XPath expression are used in combination with an XML document. Since a JSON structure is usually anonymous and doesn’t necessarily have a “root member object” JSONPath assumes the abstract name $ assigned to the outer level object.

JSONPath expressions can use the dot–notation

or the bracket–notation

Sample JSON:
use the website with below 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

Below table explains how expression works for above JSON.

JSON ExpressionUsage
$.storeTo list out all the arrays that comes under store
$.store.bookTo list all arrays under store –>book
$.store.book[1] To list first array under store –>book
$.store.book[?(@.category==’reference’)]To list all arrays under book having category= reference as value.
$.store.book[?(@.category==’fiction’ && @.price==’8.99′)] To list all arrays under book having category= reference as value and price=8.99.

For more expressions refer below links.


Leave a Reply