Friday 6 March 2020

How to query or filter Json using RestAssured JsonPath


REST Assured(RA) is a framework built on Java to test the REST services. It supports any HTTP method but has explicit support for GET, POST, PUT, DELETE, HEAD, OPTIONS, PATCH and includes specifying & validating like Headers, Cookies etc.

For validating and querying the Webservice responses, it has couple of libraries 'JsonPath' and 'XMLPath' for parsing Json and XML responses accordingly.

Even-though we know that, we have to use these libraries but most of the times we get into situations where we forget or we are not sure of the actual syntax. Here are list of few scenarios and it's JsonPath syntax (XMLPath syntax are almost similar) for your reference.

All the Jsonpath examples in this post, use the following 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":"horror",
            "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
      }
   },
   "city":"Bangalore"
}



Following examples let you know,how to effectively use the JsonPath to extract the required values from the RESTful json in different scenarios:


Sl No.ScenarioJson Path Syntax - Examples
1
Extracting a single element valuestore.bicycle.color
2Extracting a single element based on index in an arraystore.book[1].title
3Extracting a single element from the last item in the arraystore.book[-1].title
4Extracting a set of element valuesstore.book[*].title
5Extracting a list from jsonstore.book[*]
6Filtering the list based on the specific value in a nodestore.book.findAll{it.category=='reference'}
7Filtering the list based on the different values in a nodestore.book.findAll{it.category in ['reference','fiction']}
8Filtering the list based on the specific value in a node and finding number of occurrencesstore.book.findAll{it.category=='reference'}.size()
9Filtering the list using < or >store.book.findAll{it.price < 12.0}
10Filtering the list using logical operators: Using !=store.book.findAll{it.category!='reference'}
10.1Using ||store.book.findAll{it.category=='reference' || it.category=='fiction'}
10.2Using && (~between Condn.)store.book.findAll{it.category=='reference' && it.category=='fiction'}
11Filtering the list if a node contains a value which may start and end any characterstore.book.findAll{it.category=~/ref.*/ || it.category=~/icti.*/}



References:
https://github.com/rest-assured/rest-assured
https://www.javadoc.io/doc/com.jayway.restassured/rest-assured/1.6.1/com/jayway/restassured/path/json/JsonPath.html
https://support.smartbear.com/alertsite/docs/monitors/api/endpoint/jsonpath.html




No comments:

Post a Comment